博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android发送和接收短信(Broadcast receiver的一个使用例子)
阅读量:6149 次
发布时间:2019-06-21

本文共 5041 字,大约阅读时间需要 16 分钟。

hot3.png

以编程方式发送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:

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

你可能感兴趣的文章
详解 CSS 绝对定位
查看>>
AOP
查看>>
我的友情链接
查看>>
NGUI Label Color Code
查看>>
.NET Core微服务之基于Polly+AspectCore实现熔断与降级机制
查看>>
vue组件开发练习--焦点图切换
查看>>
浅谈OSI七层模型
查看>>
Webpack 2 中一些常见的优化措施
查看>>
移动端响应式
查看>>
python实现牛顿法求解求解最小值(包括拟牛顿法)【最优化课程笔记】
查看>>
js中var、let、const的区别
查看>>
腾讯云加入LoRa联盟成为发起成员,加速推动物联网到智联网的进化
查看>>
从Python2到Python3:超百万行代码迁移实践
查看>>
Windows Server已可安装Docker,Azure开始支持Mesosphere
查看>>
简洁优雅地实现夜间模式
查看>>
react学习总结
查看>>
微软正式发布PowerShell Core 6.0
查看>>
Amazon发布新的会话管理器
查看>>
InfoQ趋势报告:DevOps 和云计算
查看>>
舍弃Python,为什么知乎选用Go重构推荐系统?
查看>>