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.
}
}