Android App backend service can long-term interaction with the server to ensure the real-time data, this small project is mainly to achieve after the app exit can still run the service. Using the system's Intent.action_time_tick implementation, the broadcast of the system is broadcast every minute, you can receive the broadcast message in the program, after receiving the detection of the app service is running, if it is running, do not process, If it is not running, restart the service.
It is important to note that although this example can be used to run the service in the background, but when the user presses the home key to clear memory, still can put the app clear memory. After the app clear memory, no longer run, of course, can not achieve what function (such as receiving message push), is not up to like QQ, then, clear memory, QQ and still in the background to run.
Let's start with the implementation of this project.
We know that there are two ways to register a broadcast receiver, one is to configure it in a configuration file, and the other is to register it in code. The broadcast needs to be registered in the code. It is important to note that the registered broadcast recipient can register in the activity, but if registered in the activity, the broadcast receiver must be logged off at Ondestory, and error errors will occur if not logged off. Because you want the broadcast recipient to be able to receive the system broadcast message after the app exits, the broadcast recipient registering the system here should do so in the application class.
The code is as follows:
Public class myapplication extends application { Private StaticMyApplication MyApplication; Public MyApplication() { } Public StaticMyApplicationgetinstance(){if(MyApplication = =NULL) {MyApplication =NewMyApplication (); }returnMyApplication; }@Override Public void onCreate() {Super. OnCreate (); Intentfilter Intentfilter =NewIntentfilter (Intent.action_time_tick); Myreceiver Myreceiver =NewMyreceiver (); Registerreceiver (Myreceiver, Intentfilter); }}
After receiving the message from the system broadcast, the custom broadcast receiver Myreceiver is used for processing, in the OnReceive method detects whether the service class to be started is already running in the background, if it is running in the background, it is not processed, and if it is not running in the background, it is started. The code is as follows:
Public class myreceiver extends broadcastreceiver { Private FinalString TAG = MyReceiver.class.getSimpleName (); Public Myreceiver() { }@Override Public void OnReceive(context context, Intent Intent) {if(Intent.getaction (). Equals (intent.action_boot_completed)) {LOG.I (TAG,"Hear the boot Getaction"); }Else if(Intent.getaction (). Equals (Intent.action_time_tick)) {LOG.I (TAG,"Hear the Time_tick.");BooleanIsservicerunning =false; Activitymanager manager = (Activitymanager) context.getsystemservice (Context.activity_service); for(Runningserviceinfo service:manager.getRunningServices (Integer.max_value)) {if("Com.yin.service.MyService". Equals (Service.service.getClassName ()))full class name of//service{isservicerunning =true; LOG.I (TAG,"already started"); } }if(!isservicerunning) {Intent i =NewIntent (context, myservice.class); Context.startservice (i); LOG.I (TAG,"No boot, start now"); } }Else{LOG.I (TAG,"Listen to Other"); } }}
The code for the MyService class is given below, and the code does not perform any function
Public class myservice extends Service { Private FinalString TAG = MyService.class.getSimpleName (); Public MyService() { }@Override Public int Onstartcommand(Intent Intent,intFlagsintStartid) {flags = Start_sticky;//Use such a flag because this value causes the system to start after the service has stopped, but is not valid after clearing the memory return Super. Onstartcommand (Intent,flags,startid); }@Override Public void onCreate() {Super. OnCreate (); Notification Notification =NewNotification (); Startforeground (-1, notification); LOG.I (TAG,"OnCreate method of the MyService class"); }@Override PublicIBinderOnbind(Intent Intent) {return NULL; }@Override Public void OnDestroy() {Super. OnDestroy (); Intent Service =NewIntent ( This, Myservice.class); StartService (service);//Here is a trickery method where the service is destroyed when it is started, but it is clear that the app memory still does not work}}
The following gives the configuration file code:
<?xml version= "1.0" encoding= "Utf-8"?><manifest xmlns:android="Http://schemas.android.com/apk/res/android" package ="Com.yin.servicetest"android:versioncode="1"android:versionname ="1.0" > <uses-sdkandroid:minsdkversion="One"android:targetsdkversion ="/>" <application android: Allowbackup = "true" android:icon =< Span class= "Hljs-value" > "@drawable/ic_launcher" android:label = "@string/app_name" android:theme =" @style/apptheme " android:persistent =" true " android:name =" Com.yin.application.MyApplication "; <activityandroid:name=". Mainactivity "android:label=" @string/app_name " > <intent-filter> <action android:name="Android.intent.action.MAIN" /> <category android:name="Android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiverandroid:name="Com.yin.servicetest.MyReceiver"> </receiver> <service android:name="Com.yin.service.MyService"> </Service> </Application></manifest>
Results:
It is important to note that although that can be done in the background, it does not work if the user is aware of the memory. If you have anyone who knows QQ or how to achieve in the clear memory after how to achieve still running processes and services in the background, please do not hesitate to enlighten! Handshake
SOURCE download
Android for background service and source download