Joke Collection Website - Public benefit messages - How android Customizes Resident Broadcasting

How android Customizes Resident Broadcasting

Android broadcast mechanism means that when an application is running, a message type can be customized to allow the corresponding receiver to handle this message or system messages, such as incoming calls, short messages and messages sent by the system. Messages sent by the system can also be notified to the application by broadcasting, thus avoiding opening a new thread to monitor the status of messages sent by the system or other applications.

Classification of Android broadcasting:

1. Ordinary broadcast: This broadcast can be transmitted to each processor in turn for processing.

2. Ordered broadcast: The processing order of this broadcast by the processor is different according to the priority of the processor. The processor with high priority will intercept the message first and can delete the message.

3. Sticky message: Sticky message always exists in the message container of the system after being sent, waiting for the corresponding processor to process it. If there is no processor to process this message temporarily, it always waits in the message container.

Note: Ordinary broadcasts are different from sticky messages, but orderly broadcasts can be intercepted.

Processor registration:

1. Dynamically register in code with function code. Dynamically registered processors must be dynamically destroyed by code, and only one instance object can be used to process messages at a time.

2, static registration in the configuration file, static registration has a feature, that is, once registered, no matter whether the application is closed or opened, it will always exist in the system. This is just a rogue software virus. Every time a message is processed in static registration, it is processed by the processor of the system.

Let's take a concrete look at the sending, registering and processing process of Android broadcast messages:

① Customized processor category:

The public class MyBroadcastReceiver4 extends BroadcastReceiver {

Public MyBroadcastReceiver4() {

System. out.println ("the broadcast receiver registered by registerreceiver () has been created");

}

@ Overlay

Public void onReceive (context, intention) (

string action = intent . get action();

System. Out.println ("MyBroadcastReceiver 4 received a" +action+ "message");

if (isOrderedBroadcast()) {

System.out.println ("This is an orderly broadcast and has been intercepted." );

this . abort broadcast();

} Otherwise {

System.out.println ("This is not an orderly broadcast");

}

bundle bundle = intent . get extras();

If (bound! = null) {

System.out.println ("The data carried by this message is as follows:");

//Get a set of keys for bundle.

set set = bundle . keyset();

//Get the iterator of the above collection.

iterator iterator = set . iterator();

//Traverse the collection with an iterator

while (iterator.hasNext()) {

//Get the contents of the collection

String str =(String)iterator . next();

//Get the contents of the package

system . out . println(str+"-& gt;" +bundle . get(str));

}

} Otherwise {

System.out.println ("This message does not carry data");

}

Toasttoast = toast. Maketext (context, "MyBroadcastReceiver 4 received one".

+action+"message", toast. Length _ long);

toast . show();

//Intercept this message (remove it from the message container) so that it cannot be received by other processors.

this . abort broadcast();

}

}

② Send broadcast messages.

(1), send ordinary broadcast:

//Send a normal message

Intent Intent = new Intent(); intent . set action(" asdfasdf ");

Android _ 09 _10activity.this.sendbroadcast (intention);

(2) Send an orderly broadcast:

//Send an ordered message

Intent Intent = new Intent();

intent . set action(" asdfasdf "); Android _ 09 _ 10 activity . this . sendorderedbroadcast(intent,

null);

(3), send sticky broadcast:

//Send sticky messages

Intent Intent = new Intent();

intent . set action(" qwerqwer "); Android _ 09 _ 10 activity . this . sendstickybroadcast(intent);

③ Register the broadcast receiver.

(1) dynamic registration:

//Register the broadcast receiver.

intent filter intent filter = new intent filter(" asdfasdf ");

intent filter . set priority(0);

Android _ 09 _ 10 activity . this . register receiver(MBR 2,

intent filter);

(2) Static registration:

& ltreceiver Android:name = " . mybroadcastreceiver 4 " & gt;

& lt intends to filter Android: priority = "1000" >

& ltaction Android:name = " Android . intent . action . wallpaper _ CHANGED "/& gt;

& ltaction Android:name = " Android . provider . telephony . SMS _ RECEIVED "/& gt;

& ltaction Android:name = " Android . intent . action . phone _ STATE "/& gt;

& ltaction Android:name = " Android . intent . action . package _ REMOVED "/& gt;

//This sentence is special, which is unique to the above broadcast message.

& ltdata Android:scheme = " package "/& gt;

& lt category Android: name = "android.intention.category.default"/>

& lt/intent-filter & gt;

& lt/receiver & gt;

When you want to send sticky messages, you must get permission in the configuration file:

& ltuses-permission Android:name = " Android . permission . broadcast _ STICKY "/& gt;

If you want to use a custom processor to process system broadcasts, you must also declare access rights in the registration file, for example:

& ltuses-permission Android:name = " Android . permission . receive _ SMS "/& gt;

& ltuses-permission Android:name = " Android . permission . read _ PHONE _ STATE "/& gt;