首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Android4.1~ 5.1.1中,前台服务将被立即杀死,当app被杀死时

在Android4.1~ 5.1.1中,前台服务将被立即杀死,当app被杀死时
EN

Stack Overflow用户
提问于 2016-07-27 10:44:34
回答 3查看 792关注 0票数 2

我有一项服务正在下载前台的文件

代码语言:javascript
运行
复制
@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应用程序,但它并没有终止它的服务。我怎样才能做到这一点?谢谢。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-08-31 10:49:19

解决方案是在服务的onTaskRemoved()方法中启动一个虚拟活动,以防止服务被杀死。在启动后立即完成DummyActivity。

检查以下代码:-

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
public class DummyActivity extends Activity {
    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        finish();
    }
}

AndroidManifest条目DummyActivity :-

代码语言:javascript
运行
复制
            <activity
            android:name=".downloader.DummyActivity"
            android:allowTaskReparenting="true"
            android:alwaysRetainTaskState="false"
            android:clearTaskOnLaunch="true"
            android:enabled="true"
            android:excludeFromRecents="true"
            android:finishOnTaskLaunch="true"
            android:noHistory="true"
            android:stateNotNeeded="true"
            android:theme="@android:style/Theme.NoDisplay" />
票数 0
EN

Stack Overflow用户

发布于 2016-07-27 10:48:47

您是NOT_STICKY param的startinf服务。您应该返回START_STICKY You ()

代码语言:javascript
运行
复制
@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;
    }

来自文件:

STICKY

  • START_STICKY:系统将尝试在服务终止时再次启动服务。
  • START_NO_STICKY:系统不会关心服务是否被终止。
票数 0
EN

Stack Overflow用户

发布于 2017-02-19 17:59:58

当进程处于睡眠模式时,Android如果没有被列为受保护的应用程序,就会停止进程。转到Settings->受保护的应用程序->选择应用程序并启用为受保护的应用程序。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38610748

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档