下面贴上代码:
package com.hello; import android.app.Activity; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.telephony.SmsManager; public class HelloWorldActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String action="com.pocketdigi"; sendReceiver receiver=new sendReceiver(); IntentFilter filter=new IntentFilter(); filter.addAction(action); registerReceiver(receiver,filter); //必须先注册广播接收器,否则接收不到发送结果 SmsManager smsMgr = SmsManager.getDefault(); Intent intent = new Intent(action); PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, 0); smsMgr.sendTextMessage("10086", null, "1561", pi, null); //参数分别为号码,短信服务中心号码(null即可),短信内容,短信发送结果广播PendingIntent,短信到达广播 //关于短信到达广播(对方接收到短信时广播),据网上说,中国移动有,中国联通没有,有兴趣的同学自己试试,没兴趣直接null } class sendReceiver extends BroadcastReceiver{ //写个接收器 @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub int resultCode = getResultCode(); if(resultCode==Activity.RESULT_OK){ System.out.println("发送成功"); }else{ System.out.println("发送失败"); } } } }