Service is the most similar component to activity among the four Android components. It can represent executable programs.
The difference between Service and Activity is:(1) The Service has been running in the background and there is no user interface. (2) once the service is started, it is the same as the activity. Has its own lifecycle. Therefore, no activity is allowed.
Two steps are required for service development:(1) define a subclass that inherits the Service (2). Configure the service in androidmanifest. xml. The process is the same as configuring the activity.
There are two ways to run the Service:[1]. Use the startservice () method of context to start a service. There is no association between the visitor and the service. Even if the visitor exits, the service still runs.
[2]. Use the bingserive () method of context to enable the Service. The visitor and service are associated, that is, they are bound to the first node, the access exits, and the service exits. Broadcast reciver is essentially a global listener, which can be used for communication between components. It is used to receive the broadcast intent sent by the program, which is the same as when the application starts the activity. The service is the same as when the program starts broadcast.
Reciver also requires two steps: [1], creating the broadcast reciver intent [2], calling the context sendbroadcase () or sendorderbroadcase () method to start the developed broadcastreciver. In the code demonstrated by the author below, this service and broadcast reciver are combined, and no activity is required. When the program receives a broadcast reciver, it starts the service (the service can also be started through activity ). In this example, start the service by yourself. When it is started, the broadcast will be carried out. We will receive the broadcast and then enable the service! Broadcast Program code:
Package com. keenhi. tianpei;
Import Android. content. broadcastreceiver;
Import Android. content. context;
Import Android. content. intent;
Public class test_chargereceive extends broadcastreceiver {
@ Override
Public void onreceive (context, intent ){
// Todo auto-generated method stub
Intent intent1 = new intent (context, test_chargeservice.class );
Context. startService (intent1 );
}
}
Service program code: package com. keenhi. tianpei;
Import android. app. Service;
Import android. content. Intent;
Import android. OS. IBinder;
Public class test_chargeService extends Service {
@ Override
Public IBinder onBind (Intent intent ){
// TODO Auto-generated method stub
Return null;
} @ Override
Public void onCreate ()
{System. out. println ("service create ");}
}
Manifest. xml program code: <? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "com. keenhi. tianpei"
Android: versioncode = "1"
Android: versionname = "1.0" type = "codeph" text = "/codeph">
<Uses-SDK Android: minsdkversion = "15"/>
<Uses-Permission Android: Name = "android. Permission. receive_boot_completed"/>
<Application
Android: icon = "@ drawable/ic_launcher"
Android: Label = "@ string/app_name">
<Activity
Android: Name = ". test_chageactivity"
Android: Label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
<Service android: name = "test_chargeService"> </service>
<Explorer android: name = "test_chargeReceive">
<Intent-filter>
<Action android: name = "android. intent. action. BOOT_COMPLETED"/>
</Intent-filter>
</Cycler>
</Application>
</Manifest>
If you directly push xxx.apk into system/app with adb, the program will start automatically. However, if I did a test in eclipse, I found that BroadcastReceiver could not receive the self-starting action if I didn't start an activity. The reason is unclear, it is estimated that android never prompts the user to prevent the trojan from running in the background. Useless Activity code:
Package com. keenhi. tianpei;
Import android. app. Activity;
Import android. OS. Bundle;
Public class Test_chageActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
}
}
As you can see, this activity is absolutely useless. It will only be opened once when eclipse loads the program into the machine. After countless shutdown times, it will not be called, however, the service will use BroadcastReceiver. However, there must be activity during testing. Remember.
After testing the xxx.apk file, we can remove the activity and directly push it into the system/APP program to run in good background. Because there is a service, we can see the processes and services running in the background through the process controller (Setup/application. But in fact, we can avoid service. Only one broadcastreceiver can be used. However, please note that there is no service or process at this time. We cannot find it in the process controller, but it does run. It loads itself as part of the system program.
During the test, you still need to create an activity. To push the activity to system/app, you only need a broadcastreceiver. A broadcastreceiver program is the same as above. You can simply remove the service.
In addition, such a program will usually implement the same daemon process as the system, so that it cannot be killed. Add the following content to the manifest. xml file:
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: shareduserid = "android. uid. System">
<Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name"
Android: allowclearuserdata = "false" Android: Process = "system"
Android: killafterrestore = "false">