Receive one-time broadcast and enable permanent listening for the service

Source: Internet
Author: User

If the system only sends one-time broadcast (such as boot), the receiver only recognizes the broadcast once because the system does not send the broadcast any more; in this case, you need to enable the Service (start the service at startup) and continuously listen for changes (for example, when the network changes, the service starts). How can this problem be solved?

 

Step 1: register the broadcast receiver in manifest,

<Cycler Android: Name = ". Service. clientupdatereceiver">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. boot_completed"/> // broadcast command

<Category Android: Name = "android. Intent. Category. Home"/>
</Intent-filter>
</Cycler>

Step 2: Compile the broadcast receiver in the code,

Public class clientupdatereceiver extends broadcastreceiver
{

@ Override
Public void onreceive (context, intent)
{

Intent I = new intent ();
I. setaction ("zte.com. Market. Service ");
I. setflags (intent. flag_activity_new_task );

Context. startservice (I );
}

}

Step 3: Write a service class, where you need to write another broadcast receiver to facilitate real-time Broadcast Reception,

Public class UpdateService extends Service
{
Private connectivitymanager;
Private networkinfo Info;

Private broadcastreceiver mreceiver = new broadcastreceiver (){

@ Override
Public void onreceive (context, intent ){
String action = intent. getaction ();
If (action. Equals (connectivitymanager. connectivity_action )){
Log. D ("jeny", "the network status has changed ");
Connectivitymanager = (connectivitymanager)
Getsystemservice (context. connectivity_service );
Info = connectivitymanager. getactivenetworkinfo ();
If (info! = NULL & info. isavailable ()){
String name = info. gettypename ();
Log. D ("jeny", "current network name:" + name );
Intent I = new intent ();
I. setaction ("zte.com. Market. Service ");
I. setflags (intent. flag_activity_new_task );

Context. startservice (I); // enable the Service
} Else {
Log. D ("jeny", "no available network ");
}
}
}
};

@ Override
Public ibinder onbind (intent arg0)
{
// Todo auto-generated method stub
Return NULL;
}

@ Override
Public void ondestroy ()
{
// Todo auto-generated method stub
Super. ondestroy ();
Log. V ("jeny", "[UpdateService]... ondestroy ");
}

@ Override
Public int onstartcommand (intent, int flags, int startid)
{
// Todo auto-generated method stub

Connectivitymanager manager = (connectivitymanager) getsystemservice (context. connectivity_service );
Networkinfo mobileinfo = manager. getnetworkinfo (connectivitymanager. type_mobile );
Networkinfo wifiinfo = manager. getnetworkinfo (connectivitymanager. type_wifi );
Log. V ("jeny", "mobileinfo. isconnected () -------> "+ mobileinfo. isconnected () + "wifiinfo. isconnected () ----------> "+ wifiinfo. isconnected ());
If (mobileinfo. isconnected () | wifiinfo. isconnected ()){

}
Return super. onstartcommand (intent, flags, startid );
}

@ Override
Public void oncreate ()
{
// Todo auto-generated method stub
Super. oncreate ();
Intentfilter mfilter = new intentfilter ();
Mfilter. addaction (connectivitymanager. connectivity_action );
Registerreceiver (mreceiver, mfilter); // register the receiver after the service is enabled for the first time at startup.
}

}

 

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.