Android-Broadcast receiving

Source: Internet
Author: User

Broadcast is one of the four Android components and is a widely used mechanism for transmitting information between applications. The most typical example is: "We use radio stations as an example. We usually use radio reception as follows: many different radio stations send their content at a specific frequency, our users only need to adjust the frequency to the same as those of the broadcast station to listen to their content. The Broadcast Mechanism in Android is similar to this ." The content sent by the radio station is speech, and the broadcast content we want to send in Android is an Intent. This Intent can carry the data we want to transmit. The radio station sends content through a high-power transmitter, while Android uses the sendBroadcast method. The user accepts the content of the station by adjusting to the specific frequency of the station. In Android, the content to be broadcasted is received by registering a BroadCastReceiver. Only when the send broadcast action is the same as the receive broadcast action can the receiver accept the broadcast. We use the code to talk about the implementation configuration file of ipdialing: <er android: name = "com. iidcdut. ipcall. outcallcycler "> <intent-filter> <action android: name =" android. intent. action. NEW_OUTGOING_CALL "/> </intent-filter> </javaser> onCreate mainly configures SharePrefence and sets the IP address to be added when dialing, then BroadcastReceiver adds an IP address to the number to be dialed out. Copy the public class MainActivity extends Activity {private EditText edit_ipnumber; private SharedPreferences sp; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); edit_ipnumber = (EditText) findViewById (R. id. edit_ipnumber); sp = getSharedPreferences ("config", MODE_PRIVATE); edit_ipnumber.setText (sp. getString ("ip Number "," ");} public void click (View view) {String ipnumber = edit_ipnumber.getText (). toString (); Editor editor = sp. edit (); editor. putString ("ipnumber", ipnumber); editor. commit (); Toast. makeText (this, "set OK", Toast. LENGTH_LONG ). show () ;}} copy the code broadcast receiver: copy the code public class OutCallReceiver extends BroadcastReceiver {@ Override public void onReceive (Context context, Intent intent) {System. out. println ("OnReceive Discovers New outbound calls"); String number = getResultData (); System. out. println ("number =" + number); SharedPreferences sp = context. getSharedPreferences ("config", Context. MODE_PRIVATE); String ipnumber = sp. getString ("ipnumber", ""); String newnumber = ipnumber + number; setResultData (newnumber );}} the text message interception function of code replication is available in apps like 360. It is actually implemented through broadcast. Configuration File: <er android: name = "com. yydcdut. smslistener. smsReceiver "> <intent-filter> <action android: name =" android. provider. telephony. SMS_RECEIVED "/> </intent-filter> </javaser> broadcast: copy the public class SmsReceiver extends BroadcastReceiver {@ Override public void onReceive (Context context, Intent intent) {System. out. println ("receive SMS"); Object [] pdus = (Object []) intent. getExtras (). get ("pdus"); for (Object pdu: pdus) {SmsMessage smsmsmessage = smsMessage. createFromPdu (byte []) pdu); String body = smsMessage. getMessageBody (); String sender = smsMessage. getOriginatingAddress (); System. out. println ("body" + body); System. out. println ("sender" + sender); abortBroadcast ();}}}

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.