Getting started with Service in Android

Source: Internet
Author: User

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.

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.