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