Androidmanifest.xml
Sign up for service
<ServiceAndroid:name= ". Service.myservice"android:enabled= "true" > <Intent-filter> <ActionAndroid:name= "Com.test.service" /> </Intent-filter> </Service>
Start Services in activity (note: Localservice.class inherits from service)
Method 1
// Start the service Intent intent=New Intent (This, LocalService. Class); StartService (intent);
Method 2
LocalService localservice=NULL; //start the service with the Bindservice method Private voidBinderservice () {Intent Intent=NewIntent ( This, LocalService.class); Bindservice (Intent,Newserviceconnection () {@Override Public voidonserviceconnected (componentname componentname, IBinder binder) {//call the Bindservice method to start the service, if the service needs to interact with the activity,//The Onbind method returns the IBinder and returns the current local serviceLocalservice=( (Localservice.localbinder) binder). GetService (); //Here you can either prompt the user, or invoke some method of the service} @Override Public voidonservicedisconnected (componentname componentname) {LocalService=NULL; //Here you can prompt the user}}, Context.bind_auto_create); }
Localservice.java
Android Service Learning