您的位置:首页 > 移动开发 > Android开发

android手机短信发送

2015-04-12 20:39 330 查看
刚学android不久,最近卫卫发给我了一个视频,是怎样制作android手机上的短信发送器,一方面我感觉很有意思,另一方可以陪着她一起,今天弄完了,特来总结一下,虽然说比较简单吧,但还是有总结的必要的。
短信发送器主要是用于android下的短信发送的,其主要的界面就是输入发送母的人的号码和要发的信息的内容,主要界面有两个TextView和两个EditText还有一个Button(在import android.widget.*),下面是布局文件(layout):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/number"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/number"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/content"
/>

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minLines="3"
android:id="@+id/context"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:id="@+id/button"
/>
</LinearLayout>
以上布局文件使用的string的值需要在value/string.xml进行设置,主要的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">smsTest</string>
<string name="action_settings">Settings</string>
<string name="content">请输入短信内容</string>
<string name="number">请输入手机号</string>
<string name="button">发送信息</string>
<string name="success">发送成功</string>

</resources>
以上就是布局,可以到AVD中试试是否成功,但是我们要完成这个短信发送器还需要在主程序.java写上相应的处理。
package com.example.smstest;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast ;
import java.util.ArrayList;//以上是需要的包
@SuppressWarnings( "deprecation" )
public class MainActivity extends Activity {

private EditText numberText ;//这个变量时用来接受布局中输入的号码
private EditText contentText ;//这个变量时用来接受布局中输入的信息
@Override
protected void onCreate(Bundle savedInstanceState) {//此类程序会从这歌开始在执行
super.onCreate(savedInstanceState);//调用父类Activity中的onCreate方法
setContentView(R.layout.activity_main);//执行布局文件
numberText = (EditText) this.findViewById( R.id.number ) ; //接受布局中输入框中的号码
contentText = (EditText) this.findViewById(R.id.context) ;//接受布局中输入框中的信息
Button button = (Button) this.findViewById(R.id.button) ;//使用Button
button.setOnClickListener( new ButtonClick() ) ;//设置Button响应的内容
}

private final class ButtonClick implements View.OnClickListener
{
@Override
public void onClick( View arg0 ) {

String number = numberText.getText().toString() ;//接受布局中输入框中的号码
String content = contentText.getText().toString();
SmsManager manger = SmsManager.getDefault();//建立短信管理对象
ArrayList<String> texts=manger.divideMessage(content) ;//如果短信过长超过一条短信的最大值那么分为多条短信发送
for ( String text : texts ) {

manger.sendTextMessage(number, null, text, null, null) ;//发送消息(第一个是目的号码,第三个是消息内容)
}

Toast.makeText(getApplicationContext(), R.string.success, Toast.LENGTH_LONG ).show() ;
//使用Toast提示短信发送成功
}

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这样就可以了,可以在两个模拟器中发送(汉字会乱码但手机不会)


但是这样仍然不能再手机中使用,因为手机中的使用的话是要有使用权限的比如允许其扣除话费,那么获得权限就需要修改/smsTest/AndroidManifest.xml中加一个权限的代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.smstest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="6"
android:targetSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.smstest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

<uses-permission android:name="android.permission.SEND_SMS"/>
</manifest>
红体字是加上去的,其余是自动生成的,到此短信发送器OK了。
2014/04/03 14:44
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  手机 android import layout
相关文章推荐