Android发送和接收短信(Broadcast receiver的一个使用例子)

以编程方式发送SMS消息:

public class MainActivity extends Activity {
 
 String SENT = "SMS_SENT";
 String DELIVERED = "SMS_DELIVERED";
 PendingIntent SentPI, delieverdPI;
 BroadcastReceiver smsSentReceiver, smsDeliveredReceiver,smsDisplayReceiver;
 
 EditText phonetxt,msgtxt;
 TextView showtxt;
 
 
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 SentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
 
 delieverdPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);
 
 phonetxt = (EditText)findViewById(R.id.phonetxt);
 msgtxt = (EditText)findViewById(R.id.Msgtxt);
 showtxt = (TextView)findViewById(R.id.showMsgtxt);
 
  //--用来将信息展示在TextView上的BroadcastReceiver,信息的内容由侦听的Intent携带--
 smsDisplayReceiver = new BroadcastReceiver() {
 @Override
 public void onReceive(Context context, Intent intent) {
 String msg = intent.getStringExtra("SMS");
 showtxt.setText(msg);
 }
 };
//注册BroadcastReceiver,让它侦听Action为"SMS_RECEIVED_ACTION"的Intent 
 registerReceiver(smsDisplayReceiver,new IntentFilter("SMS_RECEIVED_ACTION"));
 
 }
 
 @Override
 protected void onResume() {
 super.onResume();
 
 //--create the BroadcastReceiver when SMS is sent,根据Intent的ResultCode通知用户信息的发送状态---
 smsSentReceiver = new BroadcastReceiver() {
 @Override
 public void onReceive(Context context, Intent intent) {
 switch (getResultCode()) {
 case Activity.RESULT_OK:
 Toast.makeText(getBaseContext(), "SMS Sent", Toast.LENGTH_SHORT).show();
 break;
 case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
 Toast.makeText(getBaseContext(), "Generic Failure", Toast.LENGTH_SHORT).show();
 break;
 case SmsManager.RESULT_ERROR_NO_SERVICE:
 Toast.makeText(getBaseContext(), "No Service", Toast.LENGTH_SHORT).show();
 break;
 case SmsManager.RESULT_ERROR_NULL_PDU:
 Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_SHORT).show();
 break;
 case SmsManager.RESULT_ERROR_RADIO_OFF:
 Toast.makeText(getBaseContext(), "Radio OFF", Toast.LENGTH_SHORT).show();
 break;
 }
 }
 };
 
 //--create the BroadcastReceiver when SMS is delievred,根据Intent的ResultCode通知用户信息的投递状态---
 smsDeliveredReceiver = new BroadcastReceiver() {
 @Override
 public void onReceive(Context context, Intent intent) {
 switch (getResultCode()) {
 case Activity.RESULT_OK:
 Toast.makeText(getBaseContext(), "SMS delivered", Toast.LENGTH_SHORT).show();
 break;
 case Activity.RESULT_CANCELED:
 Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_SHORT).show();
 break;
 }
 }
 };
 
 
 registerReceiver(smsDeliveredReceiver, new IntentFilter(DELIVERED));
 registerReceiver(smsSentReceiver, new IntentFilter(SENT));
 
 }
 
 @Override
 protected void onPause() {
 super.onPause();
 //--解绑BroadcastReceiver--
 unregisterReceiver(smsSentReceiver);
 unregisterReceiver(smsDeliveredReceiver);
 
 }
 
 @Override
 protected void onDestroy() {
 super.onDestroy();
 unregisterReceiver(smsDisplayReceiver);
 }
 
 public void SendSMS(View v) {
 SendSMS("59832776", "Hello My friend!");
 }
 
 private void SendSMS(String PhoneNumber, String msg) {
 //--通过SmsManager发送的信息,不会被系统默认的信息程序记录--
 SmsManager sms = SmsManager.getDefault();
 //--sendTextMessage的后2个参数分别是发送后和投递后的PendingIntent--
 sms.sendTextMessage(PhoneNumber, null, msg, SentPI, delieverdPI);
 }
 
 //--通过系统的短信程序发短信(关键在于intent的Type "vnd.android-dir/mms-sms"),并不会马上发送信息,而是跳转到短信程序界面--
 public void onClickSendByApp(View view){
 Intent intent = new Intent(Intent.ACTION_VIEW);
 intent.putExtra("address",phonetxt.getText().toString());
 intent.putExtra("sms_body",msgtxt.getText().toString());
 intent.setType("vnd.android-dir/mms-sms");
 startActivity(intent);
 }
 
}





接收SMS短信消息:

public class MySMSReveiver extends BroadcastReceiver {
 @Override
 public void onReceive(Context context, Intent intent) {
 //--不再让这个传入Intent继续广播下去--
 this.abortBroadcast();
 
 Bundle bundle = intent.getExtras();
 SmsMessage[] messages = null;
 String string = "SMS from ";
 if (bundle != null) {
 Object[] pdus = (Object[]) bundle.get("pdus");
 messages = new SmsMessage[pdus.length];
 for (int i = 0; i < messages.length; i++) {
 messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
 if (i == 0) {
 string += messages[i].getOriginatingAddress();
 string += ": ";
 }
 string += messages[i].getMessageBody().toString();
 }
 
 // Toast.makeText(context, string,Toast.LENGTH_LONG).show();
 Log.d("SMS Receiver", string);
 
//--如果程序在后台,能推到前台,注意得setFlags  
 //-- FLAG_ACTIVITY_NEW_TASK 	If set, this activity will become the start of a new task on this history stack. 
 Intent mainIntent = new Intent(context,MainActivity.class);
 mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 context.startActivity(mainIntent);
 
 //--广播Action为"SMS_RECEIVED_ACTION"的Intent,对应上面的BoradcastReceiver的侦听对象--
 Intent intent_forDisplay = new Intent();
 intent_forDisplay.setAction("SMS_RECEIVED_ACTION");
 intent_forDisplay.putExtra("SMS", string);
 //--广播Intent
 context.sendBroadcast(intent_forDisplay);
 
 }
 }
}




Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.administrator.mysms" >
 
 <!--短信的发送和接收权限-->
 <uses-permission android:name="android.permission.SEND_SMS"/>
 <uses-permission android:name="android.permission.RECEIVE_SMS"/>
 <application
 android:allowBackup="true"
 android:icon="@mipmap/ic_launcher"
 android:label="@string/app_name"
 android:theme="@style/AppTheme" >
 <activity
 android:name=".MainActivity"
 android:label="@string/app_name"
 android:launchMode="singleTask">
 <!--launchMode为 "singleTask"才不至于多个实例被调用-->
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />
 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
 
  <!--tag名为receiver,priority设置的越大优先级越高越先被处理-->
 <receiver android:name=".MySMSReveiver" >
 <intent-filter android:priority="1000">
  <!--特定的action名称,接收短信Intent ->
 <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
 </intent-filter>
 </receiver>
 </application>
 
</manifest>








转载于:https://my.oschina.net/Bruce370/blog/421769

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值