Android--Broadcast receive

Source: Internet
Author: User

Broadcast is one of the four major Android components and is a widely used mechanism for transferring information between applications. The most classic examples are:

"We use radio stations to make an analogy. We usually use radio radios like this: a lot of different radio stations send their content at certain frequencies, and our users just need to tune the frequency to be the same as the radio station to listen to their content. The broadcasting mechanism in Android is similar to this. ”

    • The content on the radio is voice, and the broadcast content that we are sending in Android is a Intent. This intent can carry the data we want to transmit.
    • The radio sends content through a high-powered transmitter, and in Android it is sent by Sendbroadcast.
    • The user accepts the contents of the radio station by adjusting to the specific radio frequency. And in Android to accept the broadcast content is by registering a broadcastreceiver to receive. Only the action that sends the broadcast and the action that receives the broadcast are the recipients who can accept the broadcast.

Let's talk to the code.

Implementation of IP dialing

Configuration file:

 <  receiver  android:name  = "Com.iidcdut.ipcall.OutCallReceiver"  >  <  intent-filter  >  <  action  android:name  = "Android.intent.action.NEW_OUTGOING_CALL"  />  </ intent-filter  >  </ receiver  >  

OnCreate inside is mainly to the Shareprefence configuration, set the IP to be added when dialing, and then broadcastreceiver inside is to dial out the number to add IP operation.

 Public classMainactivityextendsActivity {PrivateEditText Edit_ipnumber; PrivateSharedpreferences sp; @Overrideprotected voidonCreate (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 ("Ipnumber", "" ")); }     Public voidClick (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 (); }}

Broadcast reception:

 Public classOutcallreceiverextendsBroadcastreceiver {@Override Public voidOnReceive (Context context, Intent Intent) {System.out.println ("OnReceive found new outgoing 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); }}

SMS Blocker

Like 360 of the app inside the spam message interception function, in fact, is achieved through the broadcast.

Configuration file:

<android:name= "Com.yydcdut.smslistener.SmsReceiver">             <>                <android:name= " Android.provider.Telephony.SMS_RECEIVED "/>            </intent-filter >        </ receiver >

Broadcast inside:

 Public classSmsreceiverextendsBroadcastreceiver {@Override Public voidOnReceive (Context context, Intent Intent) {System.out.println ("Receive SMS"); Object[] PDUs= (object[]) Intent.getextras (). Get ("PDUs");  for(Object pdu:pdus) {smsmessage smsmessage= SMSMESSAGE.CREATEFROMPDU ((byte[]) PDU); String Body=Smsmessage.getmessagebody (); String Sender=smsmessage.getoriginatingaddress (); System.out.println ("Body" +body); System.out.println ("Sender" +sender);        Abortbroadcast (); }            }}

I'm the dividing line of the king of the Land Tiger.

Source code: HTTP://PAN.BAIDU.COM/S/1DD1QX01

Use broadcast IP dialing. zip

SMS stealing (Harmony) listening (harmony) device. zip

Reprint Please specify source: Http://www.cnblogs.com/yydcdut

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.