最近有个需求,需要在不调用短信界面的情况下发送短信,现将demo列出,供有需要的朋友查看。
配置文件
AndroidManifest.xml //这是一个配置权限的文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myhc.mms"
//包名
android:versionCode="1"
//版本代号
android:versionName="1.0" > //标明版本号
<uses-sdk android:minSdkVersion="10" />
//SDK的版本号
<uses-permission android:name="android.permission.SEND_SMS" />
//授予发短信的权限
<application
android:icon="@drawable/ic_launcher"
//设置应用的图标
android:label="@string/app_name" >
//设置应用的名字
android:name=".SentMmsActivity"
//这是一个类的名字,也是一个activity
android:label="@string/app_name" >
//设置应用的名字
//过滤器
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SendReceiver" >
//接收器
<intent-filter>
<action android:name="myhc.sendBroadCastSuccess" />
</intent-filter>
</receiver>
<receiver android:name=".AcceptReceiver">
<intent-filter>
<action android:name="myhc.acceptBroadCastSuccess" />
</intent-filter>
</receiver>
</application>
</manifest>
<!--线性布局布局文件main.xml-->
<?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" >
//排列方向是垂直排列
<Button
//按钮
android:id="@+id/send"
//设置按钮的 ID
android:layout_width="fill_parent"
//宽填充父元素,既是这个表格的宽度
android:layout_height="wrap_content"
//高自定义,即是一个文本所占的高度
android:text="@string/send" />
//在String.xml文件里添加文字,方便日后修改,比直接添加文字好修改和其他的组件重用
</LinearLayout>
短信发送文件SentMmsActivity
接收发送成功信息文件SendReceiver
接收对方接收成功的文件AcceptReceiver
public class SentMmsActivity extends Activity {
//SentMmsActivity继承Activity类
private Button send;
private PendingIntent sentIntent ;
private PendingIntent receiverIntent ;
@Override
public void onCreate(Bundle savedInstanceState) {
//程序已启动就调用的onCreate方法
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//显示的主页的样式
send = (Button) findViewById(R.id.send);
//显示这个组件的样式
send.setOnClickListener(new View.OnClickListener() {
//注册button自身类的监听事件
PendingIntent sentIntent = PendingIntent.getBroadcast(
//创建一个intent对象
SentMmsActivity.this, 0, new Intent("myhc.sendBroadCastSuccess"), 0);
PendingIntent receiverIntent = PendingIntent.getBroadcast(
SentMmsActivity.this, 0, new Intent("myhc.acceptBroadCastSuccess"), 0);
@Override
public void onClick(View v) {
// 在这里写点击后发送信息的事件
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("18825080059", null, "你好", sentIntent, receiverIntent);
}
});
}
}
public class SendReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//context是是上下文,
// TODO Auto-generated method stub
if (intent.getAction().equals("myhc.sendBroadCastSuccess")) {
Toast.makeText(context, "Send Success", Toast.LENGTH_SHORT).show();
//
}
}
}
public class AcceptReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals("myhc.acceptBroadCastSuccess")) {
Toast.makeText(context, "Accept Success", Toast.LENGTH_SHORT).show();
//注意后面一定要有show()方法,否则就没有办法显示
}
}
}
这个demo非常简单,在此我只说一下几点
PendingIntent就是一个Intent的描述,我们可以把这个描述交给别的程序,别的程序根据这个描述在后面的别的时间做你安排做的事情 (By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself,就相当于PendingIntent代表了Intent)。本例中别的程序就是发送短信的程序,短信发送成功后要把intent广播出去。
函数SmsManager.sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)中参数解释:
1.destinationAddress: 收件人号码
2.scAddress: 短信中心服务号码, 这里设置为null
3.text: 发送内容
4.sentIntent: 发送短信结果状态信号(是否成功发送),new 一个Intent , 操作系统接收到信号后将广播这个Intent.此过程为异步.当短信发出时,成功的话sendIntent会把其内部的描述的intent广播出去,否则产生错误代码并通过 android.app.PendingIntent.OnFinished进行回调,这个参数最好不为空,否则会存在资源浪费的潜在问题;
5.deliveryIntent: 对方接收状态信号(是否已成功接收).是当消息已经传递给收信人后所进行的PendingIntent广播。
注意在AndroidMainFest.xml中使用权限
<uses-permission android:name="android.permission.SEND_SMS" />