Android Official Development Document Training Series Course Chinese version: Background service response Intentservice processing results

Source: Internet
Author: User

Original address: https://developer.android.com/training/run-background-service/report-status.html

This lesson mainly learns how to return execution results from Intentservice to the demand point. One recommended way to do this is to use Localbroadcastmanager, which restricts the broadcast intent to the inside of the app.

Send processing results for Intentservice

In order to send the results of intentservice processing to other components, you first need to create a intent object and place the execution results inside the intent.

The next thing to do is to send the intent you just created through Localbroadcastmanager.sendbroadcast (). However, if the intent is registered, the intent will receive the result. An instance of Localbroadcastmanager can be obtained by getinstance ().

Examples are as follows:

 Public Final  class Constants {...//Defines a custom Intent action     Public Static FinalString broadcast_action ="Com.example.android.threadsample.BROADCAST"; ...//Defines the key for the status ' Extra ' in an Intent     Public Static FinalString Extended_data_status ="Com.example.android.threadsample.STATUS"; ...} Public  class rsspullservice extends intentservice {.../ * * Creates a new Intent containing a Uri object * broadcast_action is a custom Intent ACTION */Intent localintent =NewIntent (constants.broadcast_action)//Puts the status into the Intent. PutExtra (Constants.extended_data_status, STATUS);//broadcasts the Intent to receivers in this app.Localbroadcastmanager.getinstance ( This). Sendbroadcast (localintent);..}

The next step is how to handle the received intent object.

Receive processing results from Intentservice

If you need to receive the broadcast intent, then you need to use the broadcastreceiver. Override the OnReceive () method in the Broadcastreceiver implementation class. When Localbroadcastmanager broadcasts the corresponding intent object, the method is automatically recalled.

As an example:

//Broadcast Receiver for Receiving status updates from the Intentservice  private  class  responsereceiver  extends  broadcastreceiver  { //prevents instantiation  private  downloadstatereceiver  () {} //called when the broadcastreceiver gets An Intent it's registered to receive  @ public  vo ID  onreceive  (context context, Intent Intent) {... /* * Handle Intents here. */...    }}

Once the

Broadcastreceiver is defined, the specified intent filter can be defined for it. To do this, you need to create a intentfilter. The following code demonstrates how to define a filter:

//class that displays photos  public  class  displayactivity  extends  fragmentactivity  { ... public  void  oncreate  (Bundle Statebundle) { ... super . OnCreate (Statebundle); ... //the filter ' s action is broadcast_action  Intentfilter mstatusintentfilter = new  intentfilter (constants.broadcast_ ACTION); //Adds a data filter for the HTTP scheme  mstatusintentfilter.adddatascheme ( "http" ); 

In order to register Broadcastreceiver and Intentfilter with the system, you need to obtain an instance of Localbroadcastmanager before calling its Registerreceiver () method. The following code demonstrates this process:

        // Instantiates a new DownloadStateReceiver        DownloadStateReceiver mDownloadStateReceiver =                new DownloadStateReceiver();        // Registers the DownloadStateReceiver and its intent filters        LocalBroadcastManager.getInstance(this).registerReceiver(                mDownloadStateReceiver,                mStatusIntentFilter);        ...

Broadcastreceiver can handle multiple types of intent objects at the same time, and this feature can define different code for each action, without having to define the Broadcastreceiver specifically. To define an additional intentfilter for the same broadcastreceiver, just create another intentfilter, and then register again:

        /*         * Instantiates a new action filter.         * No data filter is needed.         */        new IntentFilter(Constants.ACTION_ZOOM_IMAGE);        ...        // Registers the receiver with the new filter        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(                mDownloadStateReceiver,                mIntentFilter);

Sending a broadcast intent does not start or resume activity. Even if the app is in a pending state (in the background), it will also receive the intent. If the app is in a suspended state and there is a task to be notified to the user, you can do so using notification. Never start an activity to respond to a received intent broadcast.

Android Official Development Document Training Series Course Chinese version: Background service response Intentservice processing results

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.