我想让我的应用程序在用户从应用程序托盘中删除后始终运行。 终止应用后,它并不适用于后台。 我创建了MyService类,它有一个从Web下载内容的AsyncTask。它工作正常,但无法在后台运行。 我该怎么改变这个?
我的MainActivity.java是:
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 is ",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.
} finally {
// 100% guarantee that this always happens, even if
// your update method throws an exception
mHandler.postDelayed(mStatusChecker, mInterval);
}
}
};
void startRepeatingTask() {
mStatusChecker.run();
}
void stopRepeatingTask() {
mHandler.removeCallbacks(mStatusChecker);
}
private void addNotification(){
// code here
}
}
和RestartService.java是
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 for X86 using defaults: x86
08-23 18:24:26.132 5207-5214/? I/art: Debugger is no longer active
08-23 18:24:26.132 5207-5214/? I/art: Starting a 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: is 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 all threads took: 6.693ms
08-23 18:24:31.680 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 12.280ms
08-23 18:24:35.428 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 9.350ms
08-23 18:24:41.961 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 6.408ms
08-23 18:24:42.022 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.318ms
08-23 18:24:43.672 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 6.435ms
08-23 18:24:49.739 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.680ms
08-23 18:24:51.760 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 8.013ms
08-23 18:24:51.867 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.348ms
08-23 18:24:53.070 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 10.149ms
08-23 18:24:54.248 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 6.333ms
08-23 18:24:55.250 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.010ms
08-23 18:24:55.711 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 12.875ms
08-23 18:24:55.754 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 6.557ms
08-23 18:24:55.838 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 6.844ms
08-23 18:24:58.263 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all 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 all threads took: 5.077ms
08-23 18:25:00.277 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 16.859ms
08-23 18:25:00.972 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 10.163ms
08-23 18:25:01.248 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 28.827ms
08-23 18:25:01.280 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 12.717ms
08-23 18:25:03.273 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 7.504ms
08-23 18:25:04.417 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.724ms
08-23 18:25:04.657 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 11.631ms
08-23 18:25:04.819 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 14.049ms
08-23 18:25:05.174 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.277ms
08-23 18:25:11.821 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.136ms
08-23 18:25:11.897 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 16.121ms
08-23 18:25:11.971 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 24.500ms
08-23 18:25:12.350 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 13.583ms
08-23 18:25:12.522 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 38.124ms
08-23 18:25:12.844 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 12.975ms
08-23 18:25:13.186 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 7.236ms
08-23 18:25:13.262 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 7.541ms
08-23 18:25:13.755 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 34.887ms
08-23 18:25:13.997 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 12.700ms
08-23 18:25:14.099 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 16.761ms
08-23 18:25:14.539 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.334ms
08-23 18:25:15.829 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 22.346ms
08-23 18:25:17.123 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 18.191ms
08-23 18:25:17.324 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.432ms
08-23 18:25:17.628 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 6.359ms
08-23 18:25:17.939 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.734ms
08-23 18:25:17.993 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 15.404ms
08-23 18:25:18.025 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 13.569ms
08-23 18:25:18.079 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 6.158ms
08-23 18:25:18.145 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 13.924ms
08-23 18:25:18.176 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 18.175ms
08-23 18:25:18.957 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 15.625ms
08-23 18:25:19.199 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 12.861ms
08-23 18:25:19.276 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 16.004ms
08-23 18:25:19.678 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.485ms
08-23 18:25:19.851 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 8.641ms
08-23 18:25:19.963 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 8.687ms
08-23 18:25:20.199 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 17.084ms
08-23 18:25:20.292 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 7.772ms
08-23 18:25:20.327 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.731ms
08-23 18:25:20.427 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.360ms
08-23 18:25:20.639 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.541ms
08-23 18:25:20.832 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.922ms
08-23 18:25:20.889 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 23.291ms
08-23 18:25:20.980 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 31.347ms
08-23 18:25:20.991 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 6.328ms
08-23 18:25:21.017 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 10.429ms
08-23 18:25:21.256 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 60.391ms
08-23 18:25:21.310 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.052ms
08-23 18:25:21.328 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.033ms
08-23 18:25:21.513 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 22.208ms
08-23 18:25:21.678 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 17.023ms
08-23 18:25:22.064 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 31.070ms
08-23 18:25:25.317 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.225ms
08-23 18:25:28.827 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 8.270ms
08-23 18:25:31.334 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.234ms
08-23 18:25:32.578 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 6.678ms
08-23 18:25:36.615 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.370ms
08-23 18:25:36.707 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.945ms
08-23 18:25:38.966 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 7.444ms
08-23 18:25:40.403 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 9.669ms
08-23 18:25:40.908 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 6.284ms
08-23 18:25:40.998 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 7.747ms
08-23 18:25:41.271 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 40.258ms
08-23 18:25:42.715 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 9.205ms
08-23 18:25:46.815 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.927ms
08-23 18:25:52.409 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 7.287ms
08-23 18:25:54.991 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 6.458ms
08-23 18:25:55.439 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 7.980ms
08-23 18:25:55.541 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 8.313ms
08-23 18:25:56.845 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.306ms
08-23 18:26:01.966 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 12.206ms
08-23 18:26:03.948 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 11.944ms
08-23 18:26:04.736 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.908ms
08-23 18:26:05.642 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 8.964ms
08-23 18:26:05.859 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 7.964ms
08-23 18:26:05.998 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 8.545ms
08-23 18:26:06.166 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 12.037ms
08-23 18:26:06.419 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 6.476ms
08-23 18:26:07.923 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.327ms
08-23 18:26:09.440 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 17.883ms
08-23 18:26:14.859 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 10.090ms
08-23 18:26:16.667 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 7.004ms
08-23 18:26:17.994 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 31.565ms
08-23 18:26:18.026 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 22.893ms
08-23 18:26:18.058 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 16.270ms
08-23 18:26:18.314 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 11.872ms
08-23 18:26:21.140 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 17.740ms
08-23 18:26:24.221 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.161ms
08-23 18:26:24.527 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.450ms
08-23 18:26:24.676 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 26.055ms
08-23 18:26:28.636 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 8.103ms
08-23 18:26:28.694 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 6.885ms
08-23 18:26:29.539 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.648ms
08-23 18:26:31.540 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 8.168ms
08-23 18:26:31.555 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 11.065ms
08-23 18:26:32.043 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.302ms
08-23 18:26:32.134 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 12.615ms
08-23 18:26:35.863 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.054ms
08-23 18:26:36.467 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 6.481ms
08-23 18:26:36.491 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.896ms
08-23 18:26:37.079 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 28.854ms
08-23 18:26:37.521 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.144ms
08-23 18:26:37.561 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 9.727ms
08-23 18:26:37.970 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 6.448ms
08-23 18:26:38.029 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 7.691ms
08-23 18:26:38.232 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 69.654ms
08-23 18:26:38.719 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 6.586ms
08-23 18:26:49.084 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.198ms
08-23 18:26:49.585 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 6.021ms
08-23 18:26:51.587 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.090ms
08-23 18:26:54.093 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.229ms
08-23 18:26:59.102 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 5.488ms
08-23 18:27:10.126 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 6.304ms
08-23 18:27:10.627 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 7.219ms
08-23 18:27:12.365 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 12.545ms
08-23 18:27:13.617 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all threads took: 7.829ms
08-23 18:27:19.236 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all threads took: 6.295ms
08-23 18:27:19.281 5207-5218/com.example.shubhamrajput.notify W/art: Suspending all 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 for 5.097ms for cause Background
08-23 18:28:02.834 5207-5214/com.example.shubhamrajput.notify W/art: Suspending all 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 the service class here -->
<service android:name=".MyService" />
<service
android:name=".MyIntentService"
android:exported="false"></service>
</application>
</manifest>
答案 0 :(得分:0)
当用户从最近的任务列表中滑动应用程序时,将在您的服务中调用onTaskRemoved(如果未通过stopWithTask标志在清单中另外指定),这是重新启动服务的好地方&# 34;对于您描述的用例。
查看答案以获取详细说明:https://stackoverflow.com/a/41257604/1865583
相关的代码段如下:
@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);
}
关于您自己的解决方案,添加一些日志以查看正在发生的事情,您的onDestroy是否被呼叫,您的接收方是否正在接听电话?您是否忘记在清单中注册?
请记住,在某些情况下不保证会调用onDestroy。