Android Basics Consolidation: About Pendingintent and broadcasting

Source: Internet
Author: User

Usually use the broadcast occasions more, but the details of things, have seen also did not summarize, so that some occasions have small problems, or to the principle and attributes to be clear to use freely.

In fact, they are more lazy, first look at other people's blog, there is a concept to go to the official website to see the English can be a good understanding of some.

This supplement on a message pushes the knowledge, first list some basic knowledge, and then say that they are not enough places and problems.

As usual, move the bricks first:

1. In-depth understanding of pendingintent in Android

2. Android Service (ii)--broadcastreceiver

3. Android----Base----Eighth day----broadcasereceiver and notifications

4. Broadcastreceiver of Android Development

The key points in the 1th chapter are:

    • The difference between intent and pendingintent

A. Intent is used immediately, and pendingintent can wait until the event is triggered, Pendingintent can cancel
B. Intent terminates at the end of the procedure and Pendingintent remains in force after the completion of the procedure
C. pendingintent comes with context, and intent needs to run within a context
D. Intent runs in the original task, Pendingintent runs in the new task

2nd, the focus in 4 is:

Broadcastreceiver, as the name implies, is "broadcast receiver", which is one of the four basic components of Android, which is essentially a global listener to listen to the system's global broadcast messages. It can receive broadcasts from systems and applications.

Because Broadcastreceiver is a global listener, it makes it very easy to communicate between different components of the system. For example, the activity is communicated with the service initiated by the StartService () method, which can be accomplished by means of broadcastreceiver.

Components that match the intent after the app emits a broadcast intent may be started.

    • Two ways of registering

A. Static registration method, the system to manage receiver, and all receiver in the program, can be in the XML at a glance. broadcasts registered in this way are resident broadcasts, which means that if the application is closed and a corresponding event is triggered, the program will be run automatically by the system.
B. Dynamic registration method, hidden in the code, more difficult to find, it is important to note that before exiting the program, remember to call the Context.unregisterreceiver () method. Usually in the activity of the OnStart () to register, OnStop () inside to write off. The official reminder is that if you register in Activity.onresume (), you must log out of Activity.onpause (). The broadcast registered in this way is a very resident broadcast, that is, it follows the activity's life cycle, so we need to call the Unregisterreceiver method to remove it before the activity ends.

    • Life cycle

A Broadcastreceiver object is valid only if it is called OnReceive (Context, Intent), and when returned from the function, the object is invalid and ends the life cycle. Therefore, it can be seen from this feature that in the OnReceive (Context, Intent) function that is invoked, there is no time-consuming operation that can be performed using a thread. For time-consuming operations, it should be done in StartService. Because broadcastreceiver may be invalid when you get the results returned by other asynchronous operations.

In particular, your may not show a dialog or bind to a service from within a broadcastreceiver. For the former, you should instead use the NotificationManager API. For the latter, you can use the to Context.startService() send a command to the service.

In particular, Broadcastreceiver cannot show dialog or bind service. The former can be usedNotificationManager代替,后者可以用Context.startService()发送命令给service。

    • Ordered broadcast and unordered broadcasts

The 3rd chapter introduces the basic knowledge points of broadcasting and notification, which is also a good way to learn the brain stem diagram.

After reading the above knowledge, still have questions?

Official website: http://developer.android.com/reference/android/content/BroadcastReceiver.html

First: If the broadcast is not necessary to pass between applications, it is recommended to use Localbroadcastmanager, can avoid a lot of general broadcast security issues such as the following.

Receivers used with the Context APIs is by their nature a cross-application facility, so what must consider how other Applic Ations May is able to abuse your use of them. Some things to consider is:

  • The Intent namespace is global. Make sure this Intent action names and other strings is written in a namespace do own, or else you may inadvertently con Flict with other applications.

  • The intent namespace is global, ensuring that your intent's action name and other strings are the names you specify, preventing name collisions with other apps.
  • When your use registerReceiver(BroadcastReceiver, IntentFilter) is , any application could send broadcasts to that registered receiver. You can control who can send broadcasts to it through permissions described below.

  • When registering with dynamic, any app can send a broadcast to this listener, and you can set permissions to control who can send this broadcast. Permission and user permission, the specific official website has introduced.
  • When your publish a receiver in your application's manifest and specify intent-filters for it, any other application can SE ND broadcasts to it regardless of the filters you specify. To prevent others from sending to it, make it unavailable to them with android:exported="false" .

  • As the receiver, if the static registration in the configuration file, also set the Intent-filters, the listener will also receive other apps can also send a filter-compliant broadcast, you can set android:exported= "false", Eliminate the acceptance of such broadcasts from other apps.
  • When you use sendBroadcast(Intent) the or related methods, normally any other application can receive these broadcasts. You can control who can receive such broadcasts through permissions described below. Alternatively, starting with ICE_CREAM_SANDWICH , you can also safely restrict the broadcast-a single application withIntent.setPackage

  • As a sender, when you send a broadcast, other apps generally receive these broadcasts (it may not have been set above, resulting in unwanted broadcasts from other apps), so you can specify who can receive the broadcast when you send it. For example, specify the package name and other methods.
Process Lifecycle

A process that is currently executing a broadcastreceiver (so is, currently running the code in it onReceive(Context, Intent) method) is con Sidered to be a foreground process and would be kept running by the system except under cases of extreme memory pressure.

Once return from OnReceive (), the broadcastreceiver are no longer active, and its hosting process are only as important As any and application components, that is running in it. This is especially important because if it process was only hosting the Broadcastreceiver (a common case for application s that the user have never or not recently interacted with) and then upon returning from OnReceive () the system would consider Its process is empty and aggressively kill it so the resources is available for other important processes.

This means, longer-running operations you'll often use a in Service conjunction with a broadcastreceiver to keep t He containing process active for the entire time of your operation.

The process that is executing broadcastreceiver is the foreground process, and the system retains the running state of the process unless there is an extreme memory shortage.

Once returned from OnReceive (), Broadcastreceiver is not valid, its host process and other running application have the same important level, If the host has only this one broadcastreceiver activity (such as an app that the user has never interacted with), then the host process is considered a null process when returned from OnReceive () and is easily recycled.

In other words, for time-consuming operations, the service is used as the docking guarantee operation with the broadcastreceiver process is active.

Android Basics Consolidation: About Pendingintent and broadcasting

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.