Android: Static Registration Broadcastreceiver

Source: Internet
Author: User

Because dynamic registration Broadcastreceiver can only register and receive broadcasts when the activity's OnCreate () method is called, the broadcast cannot be accepted when the program is not running, but statically registered does not depend on whether the program is running.

Of course the advantage of dynamic registration is that it is free and flexible.

Let's look at the code for static registration:

Mainactivity.java:

 PackageCom.example.staticbroadcastreceiverdemo;Importandroid.app.Activity;ImportAndroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.os.SystemClock; Public  class mainactivity extends Activity {    PrivateHandler MyHandler;//Define a intent for sending broadcasts;    PrivateIntent myintent;//Sub-thread flag bit;    Private BooleanTag =false;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.fragment_main);//instantiation of IntentMyintent =NewIntent ();//Set filter conditions;Myintent.setaction ("Com.vrinux.static");//Here I use a handler, receive a child thread every 3 seconds to send a message,        //sends a broadcast and sends the value out;MyHandler =NewHandler () {@Override             Public void Handlemessage(Message msg) {//TODO auto-generated method stub                Super. Handlemessage (msg);intCount = Msg.arg1; Myintent.putextra ("Text","Broadcast from activity ~"+ count);//Send broadcasts;Sendbroadcast (myintent); }        };NewThread (NewRunnable () {intCount =0;@Override             Public void Run() {//TODO auto-generated method stub                //Set the flag bit value to true;Tag =true; while(tag)                    {Message msg = Message.obtain ();                    MSG.ARG1 = count;                    Myhandler.sendmessage (msg); Systemclock.sleep ( the); Count + =1;    }}). Start (); }@Override    protected void OnDestroy() {//TODO auto-generated method stub        Super. OnDestroy ();//When activity is destroyed, set the flag bit to false;        //If not set to False, you will see a toast display message on the phone screen after the program exits;        //From this point can be seen: 1. Thread startup will not be destroyed as the activity destroyed;        //2. Static registration of broadcast ratio depends on whether the program is in a running state;        if(tag) {tag =false; }           }}

Planebroadcastreceiver.java:

 PackageCom.example.staticbroadcastreceiverdemo;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;ImportAndroid.content.Intent;ImportAndroid.util.Log;ImportAndroid.widget.Toast;//Inherit Broadcastreceiver class; Public  class planebroadcastreceiver extends broadcastreceiver {    @Override     Public void OnReceive(context context, Intent Intent) {//TODO auto-generated method stubLOG.I ("Mainactivity","Planebroadcastreceiver");//Get the value of intent;String Text = Intent.getstringextra ("Text");//display on the screen by toast;Toast.maketext (context, text, Toast.length_long). Show ();; }}

Androidmanifest.xml:

<?xml version= "1.0" encoding= "Utf-8"?><manifest xmlns:android="Http://schemas.android.com/apk/res/android"  package ="Com.example.staticbroadcastreceiverdemo"android:versioncode="1"  Android:versionname="1.0" >                <uses-sdkandroid:minsdkversion="android:targetsdkversion" =" />                 "    <applicationandroid:allowbackup="true"android:icon="@drawable/ Ic_launcher "android:label=" @string/app_name "android:theme=" @style/apptheme " >                                        <activityandroid:name="com.example.staticbroadcastreceiverdemo.MainActivity "Android:label=" @string/app_name " >                                    <intent-filter>                <action android:name="Android.intent.action.MAIN" />                <category android:name="Android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <!--register Broadcastreceiver and set filter and filter conditions--        <receiverandroid:name=". Planebroadcastreceiver "android:enabled=" true " >                                    <intent-filter>                <action android:name="com.vrinux.static" />            </intent-filter>        </receiver>    </Application></manifest>

Android: Static Registration Broadcastreceiver

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.