Android: Start the service, broadcast (top priority eavesdropping information) and forward it to others

Source: Internet
Author: User
Tags stub thread

3.1, service services

Service similar to activity, is actually a no interface activity, and the default does not close with the program shutdown.

Developer Custom service classes are typically used to perform some of the security software's listening capabilities, as well as message prompts, rogue software functionality.

The system service is to obtain some service management class (Xxxxmanager) of the system through similar Getsystemservice () method, to call the system to handle the function that completes oneself need, for example: telephone listens, connection state judgment and so on.

If you want to write a service class yourself, you can build a class, inherit the service, and overwrite the corresponding method.

After writing the service class, you also need to declare it in Androidmanifest.xml.

Package com.example.service;
Import Android.app.Service;
Import android.content.Intent;
Import Android.os.Handler;
Import Android.os.IBinder;
Import Android.os.Message;
    
Import Android.widget.Toast; Display toast public class Testservice extends Service {private Handler Handler every 3 seconds; private Boolean flag=true; @Override p Ublic ibinder onbind (Intent arg0) {//bind activity, generally not used, then the activity closes, service also closes//view this column more highlights: http://
Www.bianceng.cnhttp://www.bianceng.cn/OS/extra/return null;
/** Main Thread * */@Override public void OnCreate () {//TODO auto-generated Method Stub super.oncreate ();
SYSTEM.OUT.PRINTLN ("Create service"); Call Child thread Handler=new handler () {public void Handlemessage (msg) {Toast.maketext (Testservice.this, "
Started the service------------", Toast.length_long). Show ();
}
};
@Override public void OnDestroy () {//TODO auto-generated Method Stub Super.ondestroy ();
SYSTEM.OUT.PRINTLN ("Destroy service-----------"); flag=false;//this is to shut down the dead loop process} @Override public int OnSTartcommand (Intent Intent, int flags, int startid) {//This method is automatically invoked when the service is started thread t=new thread () {public void Run () (FLA
g) {try {thread.sleep (3000);} catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();
} handler.sendemptymessage (0);
}
}
};
T.start ();
    
Return Super.onstartcommand (Intent, flags, Startid); }
    
}

You can also read various information about the phone and the status of the network connection through system services.

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.