Getting Started with Android--broadcast (2)

Source: Internet
Author: User

Custom broadcasts

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.wkp.broadcast">    <!--declaring custom Power -    <PermissionAndroid:name= "Com.example.wkp.broadcast.MY_PEMISSION"/>    <ApplicationAndroid:allowbackup= "true"Android:icon= "@mipmap/ic_launcher"Android:label= "@string/app_name"Android:supportsrtl= "true"Android:theme= "@style/apptheme">        <ActivityAndroid:name=". Mainactivity ">            <!--<intent-filter> -                <!--<action android:name= "Android.intent.action.MAIN"/> -                <!--<category android:name= "Android.intent.category.LAUNCHER"/> -            <!--</intent-filter> -        </Activity>        <ActivityAndroid:name=". Secondactivity ">            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>        <receiverAndroid:name=". MyReceiver2 ">            <Intent-filter>                <ActionAndroid:name= "Com.example.wkp.broadcast.MY_ACTION"/>            </Intent-filter>        </receiver>         <!--Static Registration -        <receiverAndroid:name=". Myreceiver ">            <Intent-filter>                <!--action triggers broadcast when airplane mode is turned on -                <!--<action android:name= "Android.intent.action.AIRPLANE_MODE"/> -                <!--cannot be implemented temporarily when receiving SMS -                <!--<action android:name= "Android.provider.Telephony.SMS_RECEIVED"/> -                <!--<action android:name= "Android.provider.Telephony.SMS_DELIVER"/> -            </Intent-filter>        </receiver>    </Application>    <!--Turn on SMS permissions -    <uses-permissionAndroid:name= "Android.permission.SEND_SMS"></uses-permission>    <uses-permissionAndroid:name= "Android.permission.RECEIVE_SMS"></uses-permission>    <uses-permissionAndroid:name= "Android.permission.READ_SMS"></uses-permission>    <!--Custom Power -    <uses-permissionAndroid:name= "Com.example.wkp.broadcast.MY_PEMISSION"></uses-permission></Manifest>
Androidmainfest.xml

Notice that the custom power is declared

<permission android:name= "Com.example.wkp.broadcast.MY_PEMISSION"/>

Open Power

<uses-permission android:name= "Com.example.wkp.broadcast.MY_PEMISSION" ></uses-permission>

Static registration

<receiver android:name= ". MyReceiver2 ">
<intent-filter>
<action android:name= "Com.example.wkp.broadcast.MY_ACTION"/>
</intent-filter>
</receiver>
 PackageCom.example.wkp.broadcast;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.util.Log;/*** Created by WKP on 2016/9/21.*/ Public classMyReceiver2extendsbroadcastreceiver{@Override Public voidOnReceive (Context context, Intent Intent) {LOG.V ("HH", "Get Broadcast"); }}
Myreceiver2.java

Receiver

 PackageCom.example.wkp.broadcast;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.widget.Button;/*** Created by WKP on 2016/9/21.*/ Public classSecondactivityextendsActivity {PrivateButton btn=NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_second); BTN=(Button) Findviewbyid (r.id.send); Btn.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Intent Intent=NewIntent (); Intent.setaction ("Com.example.wkp.broadcast.MY_ACTION"); Sendbroadcast (Intent,"Com.example.wkp.broadcast.MY_PEMISSION"); LOG.V ("Hehe", "already send");    }        }); }}
Secondactivity.java

Click the button to send the broadcast

Ordered broadcast first defines the first receive

Abortbroadcast (); Ignore broadcast

Can be set by Android:priority priority is not written as 0

You can set whether to receive broadcasts from other apps

You can set whether the broadcast local broadcast is received by another app Localbroadcastmanager

Private Localbroadcastmanager manager= localbroadcastmanager.getinstance (this);

Localreceiver receiver=new localreceiver ();

Intentfilter filter=new Intentfilter ();

Filter.addaction ("Com.example.wkp.broadcast.MY_ACTION");

Manager.registerreceiver (Receiver,filter);

Getting Started with Android--broadcast (2)

Related Article

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.