Joke Collection Website - Public benefit messages - How android Customizes Resident Broadcasting
How android Customizes Resident Broadcasting
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;
- Previous article:Winter Solstice blessings from teachers to classmates
- Next article:Why can't I find the same city in Tik Tok?
- Related articles
- Unicom is in arrears and can't send text messages.
- The woman sent a text message asking if the boy was married. The man replied that I was married, and the girl replied, really? I wouldn't joke about such a thing, would I?
- At present, there are sporadic epidemics in many parts of the country. What provinces and cities call during the local New Year?
- How does WeChat send the QR code of collection?
- Is it safe to carve a seal on Taobao?
- Are there any good Japanese cartoons recently? It's best to have an introduction. . . .
- Lucky words for starting work
- Blessings to leaders on National Day
- What should I do if I often receive anonymous phone harassment?
- A 200-word essay on a story that moves you