Android implementation-after changing the local, start a service

Source: Internet
Author: User

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.

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.