我有一个正在下载前台文件的服务
@Override
public int onStartCommand(Intent intent, int flags, int sid){
this.sid = sid;
PendingIntent mainIntent = PendingIntent.getActivity(this, 0, MyApplication.mainIntent, 0);
Notification notification = new NotificationCompat.Builder(VuclipPrime.getInstance())
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Downloading Video")
.setContentText("Initiating...")
.setContentIntent(mainIntent)
.setAutoCancel(false)
.setOngoing(true)
.build();
startForeground(mNotificationId, notification);
if (VideoDownloadManager.currentVideoDownloadTask != null) {
VideoDownloadManager.currentVideoDownloadTask.start();
} else {
stopSelf();
}
return START_NOT_STICKY;
}
当我滑动以杀死该应用时,该服务也将被停止。 我在StackOverflow上尝试了所有现有的解决方案,但没有一个正在运行。 我尝试了YouTube应用,它并没有扼杀它的服务。我如何实现这一目标?感谢。
答案 0 :(得分:0)
您使用NOT_STICKY参数启动服务。 您应该在onStartCommand()
中返回 START_STICKY@Override
public int onStartCommand(Intent intent, int flags, int sid){
this.sid = sid;
PendingIntent mainIntent = PendingIntent.getActivity(this, 0, VuclipPrime.mainIntent, 0);
Notification notification = new NotificationCompat.Builder(VuclipPrime.getInstance())
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Downloading Video")
.setContentText("Initiating...")
.setContentIntent(mainIntent)
.setAutoCancel(false)
.setOngoing(true)
.build();
startForeground(mNotificationId, notification);
if (VideoDownloadManager.currentVideoDownloadTask != null) {
VideoDownloadManager.currentVideoDownloadTask.start();
} else {
stopSelf();
}
return START_STICKY;
}
来自文档:
https://developer.android.com/reference/android/app/Service.html#START_STICKY
答案 1 :(得分:0)
解决方案是在服务的onTaskRemoved()方法中启动虚拟活动,以防止服务被杀死。 启动后立即完成DummyActivity。
检查以下代码: -
int
Z* [5]
void (*)(int, char)
DummyActivity: -
package com.your.application.services;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
public class DownloadService extends Service {
int mNotificationId = 001;
Notification notification;
private int sid;
public DownloadService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int sid) {
this.sid = sid;
PendingIntent mainIntent = PendingIntent.getActivity(this, 0, YourApplication.getInstance().downloadService.mainIntent, 0);
notification = new NotificationCompat.Builder(YourApplication.getInstance())
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Starting Service")
.setContentText("Initiating...")
.setContentIntent(mainIntent)
.setAutoCancel(true)
.setOngoing(true)
.build();
startForeground(mNotificationId, notification);
if (YourApplication.getInstance().downloadService.videoDownloader != null) {
YourApplication.getInstance().downloadService.startDonwloadThread(YourApplication.getInstance().downloadService.videoDownloader);
} else {
stopSelf();
}
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1 && Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) {
Intent intent = new Intent(this, DummyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
}
DummyActivity的AndroidManifest条目: -
public class DummyActivity extends Activity {
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
finish();
}
}
答案 2 :(得分:0)
当进程处于睡眠模式时,如果它未被列为受保护的应用程序,则会停止进程。转到设置 - &gt;受保护的应用 - &gt;选择您的应用并启用为受保护的应用。