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

2019独角兽企业重金招聘Python工程师标准>>> 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:



 
 
 
 
 
 
 
 
 
 
 
 
 
  
 
 
  
                    

你可能感兴趣的:(Android发送和接收短信(Broadcast receiver的一个使用例子))