Android App Background Service report working status instance _android

Source: Internet
Author: User

This section covers the work requests running in the background service, and how to report status to the sending requestor. It is recommended to use Localbroadcastmanager to send and receive status, which restricts the ability of only this app to receive broadcasts.

Report status from Intentservice

To send a work request status from Intentservice to another component, first create a intent that contains state and data. You can also add action and URI to intent.

Next, call the Localbroadcastmanager.sendbroadcast () send intent, which receives all the receivers registered to receive the broadcast in the application. Localbroadcastmanager.getinstance () Gets the Localbroadcastmanager instance.

Copy Code code as follows:

Public final class Constants {
...
Defines a custom Intent action
public static final String broadcast_action =
"Com.example.android.threadsample.BROADCAST";
...
Defines the key for the status "Extra" in a Intent
public static final String 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 =
New Intent (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 to receive the broadcast and process it.

Receive a broadcast from Intentservice

Receive the same way as ordinary broadcast, using a broadcastreceiver subclass to implement the Broadcastreceiver.onreceive () method

Copy Code code as follows:

Broadcast receiver for receiving status updates from the Intentservice
Private class Responsereceiver extends Broadcastreceiver
{
Prevents instantiation
Private Downloadstatereceiver () {
}
Called when the broadcastreceiver gets a Intent it ' s registered to receive
@
public void OnReceive (context context, Intent Intent) {
...
/*
* Handle Intents here.
*/
...
}
}

After defining the receiver class, define the filter to match the specified action,categorie,data.
Copy Code code as follows:

/class This displays photos
public class Displayactivity extends entactivity {
    ...
    public void onCreate (Bundle statebundle) {
        ... br>         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
  & nbsp;     mstatusintentfilter.adddatascheme ("http");
        ...

The registration method is slightly different, with Localbroadcastmanager.registerreceiver ().
Copy Code code as follows:

Instantiates a new Downloadstatereceiver
Downloadstatereceiver Mdownloadstatereceiver =
New Downloadstatereceiver ();
Registers the Downloadstatereceiver and its intent filters
Localbroadcastmanager.getinstance (This). Registerreceiver (
Mdownloadstatereceiver,
Mstatusintentfilter);
...

A single broadcastreceiver can handle multiple types of broadcasts, and this feature allows you to run different code based on different action without having to define a broadcastreceiver for each action.
Copy Code code as follows:

/*
* Instantiates a new action filter.
* No data filter is needed.
*/
Statusintentfilter = new Intentfilter (constants.action_zoom_image);
...
Registers the receiver with the new filter
Localbroadcastmanager.getinstance (Getactivity ()). Registerreceiver (
Mdownloadstatereceiver,
Mintentfilter);

Sending a broadcast does not start or recover activity.broadcastreceiver allows the activity to receive processing data, including when applied in the background, but does not force the app back to the foreground. If you want the app in the background, when the user is not visible, notify the user that an event occurs, using notification. Never initiate an activity to respond to a broadcast.

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.