我有一个下载程序应用程序项目,我正在使用Service类来管理下载任务并显示如下通知:
问题:当我通过滑动关闭最近的应用程序中的应用程序时,服务将停止。我无法使用startForeground
进行通知,因为我在一项服务中有多个通知。我喜欢notify.setAutoCancel(true)
工作正常。
这是AndroidManifest.xml代码:
<service android:name=".Downloader" android:exported="false" android:enabled="true"
android:stopWithTask="false" />
这是启动服务:
public static void intentDownload(Context context , FileInfo info) {
Intent intent = new Intent(context, Downloader.class);
intent.setAction(ACTION_DOWNLOAD);
intent.putExtra(EXTRA_TAG, info.Tag);
intent.putExtra(EXTRA_APP_INFO, info);
context.startService(intent);
}
这里是onStartCommand
:
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
String action = intent.getAction();
FileInfo fInfo;
String tag = intent.getStringExtra(EXTRA_TAG);
switch (action) {
case ACTION_DOWNLOAD:
fInfo = (FileInfo) intent.getSerializableExtra(EXTRA_APP_INFO);
download(fInfo);
break;
case ACTION_PAUSE:
fInfo = (FileInfo) intent.getSerializableExtra(EXTRA_APP_INFO);
pause(fInfo);
break;
case ACTION_CANCEL:
cancel(tag);
break;
case ACTION_PAUSE_ALL:
pauseAll();
break;
case ACTION_CANCEL_ALL:
cancelAll();
break;
}
}
return Service.START_STICKY;
}
我该如何解决?!
答案 0 :(得分:1)
当您返回START_STICKY
时,只要您关闭/终止应用程序,此服务就会停止,因为在应用程序关闭后,所有Intent以及变量的所有Reference / Value都将变为null
,因此{ {1}}服务无法获取Intent值。
在应用程序终止后使用STICKY
return START_REDELIVER_INTENT
查看同样的问题Here