In android, the service and activity are automatically started when the mobile phone is started.

Source: Internet
Author: User

First, you must understand that the communication between android programs is broadcast broadcastReceiver, and the data sharing between programs is using the content provider Contentproved. Therefore, you must start the service when the mobile phone starts, you need to know when the mobile phone is turned on. At this time, you can register a broadcast to receive the action (the program broadcasts the information through the action to let the program know ), the mobile phone opened the opportunity to send an action named "android. intent. action. BOOT_COMPLETED ", as long as the receiver receives this broadcast, it can process related events in the receiver's overload method (receipt method) onReceive (Context context, Intent intent) and start the service, or start the program.

 

The following is the code of the receiver class:

Import android. content. BroadcastReceiver;
Import android. content. Context;
Import android. content. Intent;
Import android. widget. Toast;

Public class AutoService extends BroadcastReceiver
{
/* Intent source to receive */
Static final String ACTION = "android. intent. action. BOOT_COMPLETED ";

Public void onReceive (Context context, Intent intent)
{
If (intent. getAction (). equals (ACTION ))
{
Context. startService (new Intent (context, TrojanService. class); // start the countdown service.
Toast. makeText (context, "TrojanService service has started! ", Toast. LENGTH_LONG). show ();
// You can add the application code that is automatically started upon startup.
}
}
}

At the same time, the broadcast class must be described in manifest. xml.
<Cycler android: name = ". AutoService" android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. BOOT_COMPLETED"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Cycler>

At the same time, the permission to receive mobile phone startup information is allowed:
<Uses-permission android: name = "android. permission. RECEIVE_BOOT_COMPLETED"> </uses-permission>


Author: Chen Jie

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.