Android service resident, Android resident

Source: Internet
Author: User

Android service resident, Android resident

Recently, I have studied how to prevent the service from being killed. Baidu has discovered that there are basically two methods. return super in Service onStartCommand. onStartCommand (intent, START_STICKY, startId); in this way, the process will be automatically restarted immediately after it is killed with one click. 2. register the receiver, listen for boot events, and start the service in onReceive.

It is okay on the simulator. It can be started normally and cannot be killed, but it cannot be started on the mobile phone. later it was found that it was a problem with the Xiaomi system settings, in the security center, you can enable automatic start. By default, qq can be automatically started, so qq cannot be killed.

Note: The newly installed application must be started once before the registered Referer takes effect. After the application is killed or started, it will receive the broadcast, such as unlocking.


Code Attached

<? Xml version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: android = "http://schemas.android.com/apk/res/android" package = "com. example. kaijiqidong "android: versionCode =" 1 "android: versionName =" 1.0 "> <uses-sdk android: minSdkVersion =" 8 "android: targetSdkVersion =" 21 "/> <! -- Listen for boot start permission --> <uses-permission android: name = "android. permission. RECEIVE_BOOT_COMPLETED "/> <application android: allowBackup =" true "android: icon =" @ drawable/ic_launcher "android: label =" @ string/app_name "android: theme = "@ style/AppTheme"> <activity android: name = ". mainActivity "android: label =" @ string/app_name "> <intent-filter> <action android: name =" android. intent. action. MAIN "/> <category android: nam E = "android. intent. category. LAUNCHER "/> </intent-filter> </activity> <Cycler android: name =" com. example. kaijiqidong. mycycler "> <intent-filter> <! -- Uninstall the application --> <action android: name = "android. intent. action. PACKAGE_REMOVED "/> <data android: scheme =" package "/> </intent-filter> <! -- Boot --> <action android: name = "android. intent. action. BOOT_COMPLETED"/> <! -- Unlock --> <action android: name = "android. intent. action. USER_PRESENT "/> </intent-filter> </Cycler> <service android: name =" com. example. kaijiqidong. myService "> </service> </application> </manifest>


The code in MainActivity can be ignored... Package com. example. kaijiqidong; import android. app. activity; import android. content. componentName; import android. content. intent; import android. content. serviceConnection; import android. OS. bundle; import android. OS. IBinder; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. view. onClickListener; public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); findViewById (R. id. but ). setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubstartService (new Intent (getApplicationContext (), MyService. class); bindService (new Intent (getApplicationContext (), MyService. class), new ServiceConnection () {@ Overridepublic void onServiceDisconnected (ComponentName name) {// TODO Auto-generated method stub} @ Overridepublic void onServiceConnected (ComponentName name, IBinder service) {// TODO Auto-generated method stub }}, BIND_AUTO_CREATE );}});}}



Package com. example. kaijiqidong; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. widget. toast; public class MyReceiver extends BroadcastReceiver {@ Overridepublic void onReceive (Context context, Intent intent) {// TODO Auto-generated method stubSystem. out. println ("received ----" + intent. getAction (); // start the service context. startService (new Intent (context, MyService. class); Toast. makeText (context, "fu wu qi dong", 0 ). show ();}}


Package com. example. kaijiqidong; import android. app. service; import android. content. intent; import android. OS. IBinder; public class MyService extends Service {@ Overridepublic IBinder onBind (Intent intent) {// TODO Auto-generated method stubreturn null;} @ Overridepublic int onStartCommand (Intent intent, int flags, int startId) {// TODO Auto-generated method stub // return super. onStartCommand (intent, START_STICKY, startId); // ensure that the return super Service is automatically restarted after one-click cleaning. onStartCommand (intent, START_STICKY, startId) ;}@ Overridepublic void onCreate () {// TODO Auto-generated method stubsuper. onCreate (); Intent intent = new Intent (getApplicationContext (), MainActivity. class); intent. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK); startActivity (intent );}}




Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.