Android Grooming
Original link
Create a service:
The most critical is the return of the Onstartcommand inside the Service.start_sticky,
Package Com.qywanwei.servicetest2;import Android.app.notification;import Android.app.pendingintent;import Android.app.service;import Android.content.intent;import Android.graphics.bitmapfactory;import Android.os.IBinder ; import Android.support.v7.app.notificationcompat;import Android.util.log;import android.content.ComponentName; Import Android.content.context;import Android.content.pm.packageinfo;import Android.content.pm.PackageManager; Import Android.content.pm.resolveinfo;import Java.util.list;public class MyService extends Service implements Runnable {private thread thread; Public MyService () {} @Override public ibinder onbind (Intent Intent) {//Todo:return the communication Channel to the service. throw new Unsupportedoperationexception ("not yet implemented"); }//Create service time call, first create @Override public void OnCreate () {super.oncreate (); LOG.D ("MyServer", "OnCreate: Create service"); OnCreate when creating the Initialize thread = new Thread (this); Thread.Start ();//Reception service//Intent intent=new Intent (this,mainactivity.class);//Pendingintent Pendingin Tent=pendingintent.getactivity (this,0,intent,0);//Notification notification=new Notificationcompat.builder (This) . Setcontenttitle ("111")//. Setcontenttext ("a")//. Setwhen (System.currentt Imemillis ())//. Setsmallicon (R.mipmap.ic_launcher)//. Setlargeicon (Bitmapfactory.decoderesou Rce (Getresources (), r.mipmap.ic_launcher))//. Setcontentintent (Pendingintent)//. Build ();// Startforeground (1,notification); }//each time the service starts the call, each time it starts @Override public int Onstartcommand (Intent Intent, int flags, int startid) {log.d ("Myse RVer "," OnCreate: Start Service "); If the service is stopped, regenerate a new if (thread.isinterrupted ()) {thread = new thread (this); Thread.Start (); } return service.start_sticky;//return supEr.onstartcommand (Intent, flags, Startid); } @Override public void Run () {int i=0; while (true) {try {//every 10 seconds output thread.sleep (10000); Toast.maketext (Getapplicationcontext (), "service start" +i++,toast.length_long). Show (); LOG.D ("MyServer", "service Start" +i++); Openapp ("Com.qywanwei.servicetest2"); } catch (Interruptedexception e) {e.printstacktrace (); }}}//private void Openapp (View V, string packagename) {private void Openapp (string packagename) { Context context = V.getcontext (); PackageInfo pi = null; Packagemanager pm = Context.getpackagemanager (); Packagemanager pm = Getpackagemanager (); try {pi = pm.getpackageinfo (packagename, 0); } catch (Packagemanager.namenotfoundexception e) {e.printstacktrace (); } Intent resolveintent = new Intent (Intent. Action_main, NULL); Resolveintent.addcategory (Intent.category_launcher); Resolveintent.setpackage (Pi.packagename); List<resolveinfo> apps = pm.queryintentactivities (resolveintent, 0); ResolveInfo ri = Apps.iterator (). Next (); if (ri = null) {PackageName = Ri.activityInfo.packageName; String className = ri.activityInfo.name; Intent Intent = new Intent (intent.action_main); Intent.addcategory (Intent.category_launcher); ComponentName cn = New ComponentName (PackageName, className); Intent.setflags (Intent.flag_activity_new_task); Intent.setcomponent (CN); StartActivity (Intent); }}//When the service is destroyed @Override public void OnDestroy () {Super.ondestroy (); LOG.D ("MyServer", "OnCreate: Destruction Services"); }}
Android Background launches app demo every 10 seconds, starts once, never quits