<span id="Label3"></p><p><p>Recent research how to prevent the service is killed, online baidu, found that the basic two settings, 1. return Super.onstartcommand in service Onstartcommand (intent, start_sticky, startid); , so that the process is automatically restarted immediately after it is killed by a key. 2. Register receiver, monitor the boot and other events, start the service in Onreceive.</p></p><p><p><span style="white-space:pre"></span>In the simulator is not a problem, can start normally, and kill, but on the phone has been unable to start, and later found that is the problem of Xiaomi system setup, in the Security Center to allow automatic start on the line, the default has been allowed, QQ automatically start, so QQ kill not Die.</p></p><p><p><span style="white-space:pre"></span></p></p><p><p>Note: the newly installed app must be activated once to register receiver to take effect, regardless of whether the application is killed, or just boot, can be unlocked and other broadcasts</p></p><p><p><br></p></p><p><p>Attached code</p></p><p><p></p></p><pre name="code" class="html"><?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= "/>" <!--monitoring power-on start-up Privileges--<uses-pe Rmission android:name= "android.permission.RECEIVE_BOOT_COMPLETED"/> <application Android:allo wbackup= "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:name= "android.intent.category.LAUNCHER "/> </intent-filter> </activity> <receiveR android:name= "com.example.kaijiqidong.MyReceiver" > <intent-filter> <!--app uninstall--> ; <action android:name= "android.intent.action.PACKAGE_REMOVED"/> <data android:scheme= "package"/> </intent-filter> <intent-filter> <!--start-up-<ac tion android:name= "android.intent.action.BOOT_COMPLETED"/> <!--unlocked--<action A Ndroid:name= "android.intent.action.USER_PRESENT"/> </intent-filter> </receiver> <service android:name= "com.example.kaijiqidong.MyService" ></service> </application></mani Fest></pre><br><br><p><p></p></p><p><p></p></p><pre name="code" class="java">Mainactivity inside the code 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);}});}} </pre><br><br><p><p></p></p><p><p><br></p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">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 C0/>----"+intent.getaction ());//start Service Context.startservice (new Intent (context, myservice.class)); Toast.maketext (context, "fu wu qi Dong ", 0). show ();}}</pre></pre><br><br><p><p></p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">Package Com.example.kaijiqidong;import Android.app.service;import Android.content.intent;import Android.os.IBinder ;p ublic class MyService extends Service{@Overridepublic ibinder onbind (Intent Intent) {//TODO auto-generated method Stubr Eturn null;} @Overridepublic int onstartcommand (Intent Intent, int flags, int Startid) {//TODO auto-generated method Stub//return super.ons Tartcommand (intent, start_sticky, startid);//guaranteed to automatically restart service after a key cleanup return Super.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);}}</pre></pre><br><br><p><p></p></p><p><p><br></p></p><p><p><br></p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Android Service resident</p></p></span>
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