1. Define a class that inherits broadcastreceiver and is used to receive the action_locale_changed message. The Code is as follows:
Package Android. IPC;
Import Android. content. broadcastreceiver;
Import Android. content. context;
Import Android. content. intent;
Public class IPC extends broadcastreceiver {
Static final string action = "android. Intent. Action. boot_completed ";
@ Override
Public void onreceive (context, intent ){
If (intent. getaction (). compareto (intent. action_locale_changed) = 0)
{
// Start Activity
Intent smart_service = new intent (intent. action_run );
Smart_service.setclass (context, datetimeservice. Class );
// Smart_service.addflags (intent. flag_activity_new_task );
Context. startservice (smart_service );
}
}
}
2. Define a service that inherits the service and is used to complete background tasks. The Code is as follows:
Package Android. IPC;
Import Android. content. intent;
Import Android. App. Service;
Import Android. util. log;
Import Android. widget. Toast;
Import Android. OS. ibinder;
Public class datetimeservice extends Service {
Private final string tag = "datetimeservice ";
Public ibinder onbind (intent)
{
Return NULL;
}
Public void onstart (intent, int startid)
{
Super. onstart (intent, startid );
Toast. maketext (this, "Start onstart", Toast. length_long). Show ();
Log. V (TAG, "Start onstart ");
// Your task to add
Stopservice (intent );
}
Public void oncreate ()
{
Super. oncreate ();
Toast. maketext (this, "Start oncreate", Toast. length_long). Show ();
Log. V (TAG, "Start oncreate ");
}
Public void ondestroy ()
{
Super. ondestroy ();
Toast. maketext (this, "Start ondestroy", Toast. length_long). Show ();
Log. V (TAG, "Start ondestroy ");
}
}
3. register the handler in androidmenifese. xml and add intent filtering, as shown below:
<Cycler Android: Name = "IPC">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. locale_changed"/>
</Intent-filter>
</Cycler>
Register the Service as follows:
<Service android: Name = ". datetimeservice"> </service>
In this way, you can start the service to complete the desired task after the loval change.