java - App is getting killed even after using Service -




i want make app run after removed app tray user. after terminating app ,it didn't pertain in background. created myservice class has asynctask download content web. works fine unable run in background. should change int this?

my mainactivity.java is:

package com.example.shubhamrajput.notify;  import ...   public class mainactivity extends appcompatactivity {       @override     protected void oncreate(bundle savedinstancestate) {          super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         startservice(new intent(this, myservice.class));       } } 

// myservice.java

    package com.example.shubhamrajput.notify;     import ...      public class myservice extends service {         private int minterval = 2000;         private handler mhandler;         string result=null;         public class downloadtask extends asynctask<string, void, string> {                 //code here     }   }     void updatetask(){         downloadtask task = new downloadtask();         string result = null;          try {              result = task.execute("https://time.is").get();             //log.i("result ",result);             if(!result.equals(result)){                 if(result==null)                 result=result;                 else                 addnotification();             }          } catch (interruptedexception e) {              e.printstacktrace();          } catch (executionexception e) {              e.printstacktrace();          }     }      @nullable     @override     public ibinder onbind(intent intent) {         return null;     }     public int onstartcommand(intent intent,int flags,int startid){         mhandler = new handler();         startrepeatingtask();         return start_sticky;      }     @override     public void ondestroy() {         super.ondestroy();         intent restartservice = new intent("restartservice");         sendbroadcast(restartservice);      }      runnable mstatuschecker = new runnable() {         @override         public void run() {             try {                 updatetask();//this function can change value of minterval.             } {                 // 100% guarantee happens, if                 // update method throws exception                 mhandler.postdelayed(mstatuschecker, minterval);             }         }     };      void startrepeatingtask() {         mstatuschecker.run();     }      void stoprepeatingtask() {         mhandler.removecallbacks(mstatuschecker);     }     private void addnotification(){        // code here      } } 

and restartservice.java is

    package com.example.shubhamrajput.notify;  import android.content.broadcastreceiver; import android.content.context; import android.content.intent;  public class restartservice extends broadcastreceiver {      @override     public void onreceive(context context, intent intent) {          context.startservice(new intent(context,myservice.class));     } } 

logcat

18:24:26.102 5207-5207/? i/art: not late-enabling -xcheck:jni (already on) 08-23 18:24:26.103 5207-5207/? w/art: unexpected cpu variant x86 using defaults: x86 08-23 18:24:26.132 5207-5214/? i/art: debugger no longer active 08-23 18:24:26.132 5207-5214/? i/art: starting blocking gc instrumentation 08-23 18:24:26.195 5207-5207/? w/system: classloader referenced unknown path: /data/app/com.example.shubhamrajput.notify-1/lib/x86 08-23 18:24:26.204 5207-5207/? i/instantrun: starting instant run server: main process 08-23 18:24:26.212 5207-5223/? d/networksecurityconfig: no network security config specified, using platform default 08-23 18:24:28.822 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 6.693ms 08-23 18:24:31.680 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 12.280ms 08-23 18:24:35.428 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 9.350ms 08-23 18:24:41.961 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 6.408ms 08-23 18:24:42.022 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 5.318ms 08-23 18:24:43.672 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 6.435ms 08-23 18:24:49.739 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 5.680ms 08-23 18:24:51.760 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 8.013ms 08-23 18:24:51.867 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 5.348ms 08-23 18:24:53.070 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 10.149ms 08-23 18:24:54.248 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 6.333ms 08-23 18:24:55.250 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 5.010ms 08-23 18:24:55.711 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 12.875ms 08-23 18:24:55.754 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 6.557ms 08-23 18:24:55.838 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 6.844ms 08-23 18:24:58.263 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 6.099ms                                                                       [ 08-23 18:24:58.319  3533: 3545 d/         ]                                                                      hostconnection::get() new host connection established 0x8477eac0, tid 3545                                                                        [ 08-23 18:24:58.320  3533: 3545 w/         ]                                                                      unrecognized gles max version string in extensions: android_emu_checksum_helper_v1 android_emu_dma_v1  08-23 18:25:00.254 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 5.077ms 08-23 18:25:00.277 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 16.859ms 08-23 18:25:00.972 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 10.163ms 08-23 18:25:01.248 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 28.827ms 08-23 18:25:01.280 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 12.717ms 08-23 18:25:03.273 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 7.504ms 08-23 18:25:04.417 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 5.724ms 08-23 18:25:04.657 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 11.631ms 08-23 18:25:04.819 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 14.049ms 08-23 18:25:05.174 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 5.277ms 08-23 18:25:11.821 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 5.136ms 08-23 18:25:11.897 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 16.121ms 08-23 18:25:11.971 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 24.500ms 08-23 18:25:12.350 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 13.583ms 08-23 18:25:12.522 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 38.124ms 08-23 18:25:12.844 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 12.975ms 08-23 18:25:13.186 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 7.236ms 08-23 18:25:13.262 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 7.541ms 08-23 18:25:13.755 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 34.887ms 08-23 18:25:13.997 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 12.700ms 08-23 18:25:14.099 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 16.761ms 08-23 18:25:14.539 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 5.334ms 08-23 18:25:15.829 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 22.346ms 08-23 18:25:17.123 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 18.191ms 08-23 18:25:17.324 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 5.432ms 08-23 18:25:17.628 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 6.359ms 08-23 18:25:17.939 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 5.734ms 08-23 18:25:17.993 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 15.404ms 08-23 18:25:18.025 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 13.569ms 08-23 18:25:18.079 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 6.158ms 08-23 18:25:18.145 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 13.924ms 08-23 18:25:18.176 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 18.175ms 08-23 18:25:18.957 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 15.625ms 08-23 18:25:19.199 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 12.861ms 08-23 18:25:19.276 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 16.004ms 08-23 18:25:19.678 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 5.485ms 08-23 18:25:19.851 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 8.641ms 08-23 18:25:19.963 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 8.687ms 08-23 18:25:20.199 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 17.084ms 08-23 18:25:20.292 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 7.772ms 08-23 18:25:20.327 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 5.731ms 08-23 18:25:20.427 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 5.360ms 08-23 18:25:20.639 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 5.541ms 08-23 18:25:20.832 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 5.922ms 08-23 18:25:20.889 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 23.291ms 08-23 18:25:20.980 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 31.347ms 08-23 18:25:20.991 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 6.328ms 08-23 18:25:21.017 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 10.429ms 08-23 18:25:21.256 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 60.391ms 08-23 18:25:21.310 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 5.052ms 08-23 18:25:21.328 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 5.033ms 08-23 18:25:21.513 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 22.208ms 08-23 18:25:21.678 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 17.023ms 08-23 18:25:22.064 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 31.070ms 08-23 18:25:25.317 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 5.225ms 08-23 18:25:28.827 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 8.270ms 08-23 18:25:31.334 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 5.234ms 08-23 18:25:32.578 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 6.678ms 08-23 18:25:36.615 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 5.370ms 08-23 18:25:36.707 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 5.945ms 08-23 18:25:38.966 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 7.444ms 08-23 18:25:40.403 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 9.669ms 08-23 18:25:40.908 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 6.284ms 08-23 18:25:40.998 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 7.747ms 08-23 18:25:41.271 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 40.258ms 08-23 18:25:42.715 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 9.205ms 08-23 18:25:46.815 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 5.927ms 08-23 18:25:52.409 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 7.287ms 08-23 18:25:54.991 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 6.458ms 08-23 18:25:55.439 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 7.980ms 08-23 18:25:55.541 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 8.313ms 08-23 18:25:56.845 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 5.306ms 08-23 18:26:01.966 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 12.206ms 08-23 18:26:03.948 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 11.944ms 08-23 18:26:04.736 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 5.908ms 08-23 18:26:05.642 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 8.964ms 08-23 18:26:05.859 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 7.964ms 08-23 18:26:05.998 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 8.545ms 08-23 18:26:06.166 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 12.037ms 08-23 18:26:06.419 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 6.476ms 08-23 18:26:07.923 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 5.327ms 08-23 18:26:09.440 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 17.883ms 08-23 18:26:14.859 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 10.090ms 08-23 18:26:16.667 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 7.004ms 08-23 18:26:17.994 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 31.565ms 08-23 18:26:18.026 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 22.893ms 08-23 18:26:18.058 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 16.270ms 08-23 18:26:18.314 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 11.872ms 08-23 18:26:21.140 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 17.740ms 08-23 18:26:24.221 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 5.161ms 08-23 18:26:24.527 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 5.450ms 08-23 18:26:24.676 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 26.055ms 08-23 18:26:28.636 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 8.103ms 08-23 18:26:28.694 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 6.885ms 08-23 18:26:29.539 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 5.648ms 08-23 18:26:31.540 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 8.168ms 08-23 18:26:31.555 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 11.065ms 08-23 18:26:32.043 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 5.302ms 08-23 18:26:32.134 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 12.615ms 08-23 18:26:35.863 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 5.054ms 08-23 18:26:36.467 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 6.481ms 08-23 18:26:36.491 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 5.896ms 08-23 18:26:37.079 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 28.854ms 08-23 18:26:37.521 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 5.144ms 08-23 18:26:37.561 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 9.727ms 08-23 18:26:37.970 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 6.448ms 08-23 18:26:38.029 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 7.691ms 08-23 18:26:38.232 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 69.654ms 08-23 18:26:38.719 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 6.586ms 08-23 18:26:49.084 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 5.198ms 08-23 18:26:49.585 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 6.021ms 08-23 18:26:51.587 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 5.090ms 08-23 18:26:54.093 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 5.229ms 08-23 18:26:59.102 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 5.488ms 08-23 18:27:10.126 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 6.304ms 08-23 18:27:10.627 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 7.219ms 08-23 18:27:12.365 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 12.545ms 08-23 18:27:13.617 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 7.829ms 08-23 18:27:19.236 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 6.295ms 08-23 18:27:19.281 5207-5218/com.example.shubhamrajput.notify w/art: suspending threads took: 9.944ms                                                                       [ 08-23 18:27:19.286  8988: 9067 d/         ]                                                                      hostconnection::get() new host connection established 0x99cfe5c0, tid 9067                                                                        [ 08-23 18:27:19.287  8988: 9067 w/         ]                                                                      unrecognized gles max version string in extensions: android_emu_checksum_helper_v1 android_emu_dma_v1  08-23 18:27:33.060 5207-5218/com.example.shubhamrajput.notify i/art: waitforgctocomplete blocked 5.097ms cause background 08-23 18:28:02.834 5207-5214/com.example.shubhamrajput.notify w/art: suspending threads took: 5.699ms 

android manifest:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.example.shubhamrajput.notify">      <uses-permission android:name="android.permission.internet" />      <application         android:allowbackup="true"         android:icon="@mipmap/ic_launcher"         android:label="@string/app_name"         android:roundicon="@mipmap/ic_launcher_round"         android:supportsrtl="true"         android:theme="@style/apptheme">         <activity android:name=".mainactivity">             <intent-filter>                 <action android:name="android.intent.action.main" />                  <category android:name="android.intent.category.launcher" />             </intent-filter>         </activity>         <!-- defining service class here -->         <service android:name=".myservice" />         <service             android:name=".myintentservice"             android:exported="false"></service>     </application>  </manifest> 

when user swipes app recent task list, ontaskremoved invoked in service (if not specified otherwise in manifest via stopwithtask flag) , "good place restart service" use case described.

check answers detailed explanation: https://stackoverflow.com/a/41257604/1865583

the relevant code snippet follows:

@override public void ontaskremoved(intent rootintent){     intent restartservicetask = new intent(getapplicationcontext(),this.getclass());     restartservicetask.setpackage(getpackagename());         pendingintent restartpendingintent =pendingintent.getservice(getapplicationcontext(), 1,restartservicetask, pendingintent.flag_one_shot);     alarmmanager myalarmservice = (alarmmanager) getapplicationcontext().getsystemservice(context.alarm_service);     myalarmservice.set(             alarmmanager.elapsed_realtime,             systemclock.elapsedrealtime() + 1000,             restartpendingintent);      super.ontaskremoved(rootintent); } 

regarding own solution, add logs see going on, ondestroy being called, receiver receiving call? did maybe forget register in manifest?

keep in mind ondestroy not guaranteed called in cases.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -