As one of the five basic components of Android, Service is very powerful and basic functions are not complex.
It is necessary to use the service to implement a trojan in Android. It can be run in the background and started. The following implementation is simple:
1. Create a new Android project HelloService in Eclipse
2. In the ServiceAndroid project-> New-> Class Package, select ServiceAndroid Name to write to MyService.
3. Open MyService. java and enter the following content:
Package Service. HelloService;
Import android. app. Service;
Import android. content. Intent;
Import android. OS. IBinder;
Import android. util. Log;
Public class MyService extends Service {
Private boolean threadDisable;
Private int count;
@ Override
Public IBinder onBind (Intent intent ){
Return null;
}
@ Override
Public void onCreate (){
Super. onCreate ();
New Thread (new Runnable (){
@ Override
Public void run (){
While (! ThreadDisable ){
Try {
Thread. sleep (1000 );
} Catch (InterruptedException e ){
}
Count ++;
Log. v ("MyService", "Count is" + count );
}
}
}). Start ();
}
@ Override
Public void onDestroy (){
Super. onDestroy ();
This. threadDisable = true;
Log. v ("CountService", "on destroy ");
}
Public int getCount (){
Return count;
}
}
4. Modify the AndroidManifest. xml file of the project and add a registration service.
<Service android: name = "MyService"/>
5. Modify the project's main file Service. java code as follows:
Package Service. HelloService;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. content. Intent;
Public class Service extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
This. startService (new Intent (this, MyService. class ));
}
@ Override
Protected void onDestroy (){
Super. onDestroy ();
This. stopService (new Intent (this, MyService. class ));
}
}
Now, compile and run the Android simulator. Open LogCat to view the debugging information.