后台服务在奥利奥中不起作用 [英] Background service is not working in Oreo

查看:21
本文介绍了后台服务在奥利奥中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我也杀死应用程序实例,我想在后台运行我的应用程序.但是在我杀死我的应用程序后,该服务也停止工作.这是我的代码,请任何人帮助我解决我的问题.

I want to run my app in background if I kill the app instance also. But after I kill my app the service also stops working. Here is my code please any one help me to solve my issue.

我按照此链接在后台运行,但如果我删除实例,它就无法工作.如果实例也被删除,谁能告诉我如何运行后台服务?

I followed this link for running in the background but it is not working if I remove the instance. Please can any one show me how to run a background service if the instance is removed also?

这是我的 MainActivity

This is my MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ctx = this;
    setContentView(R.layout.activity_main);
    Intent alarmIntent = new Intent(MainActivity.this, AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(MainActivity.this, ALARM_REQUEST_CODE, alarmIntent, 0);
    mSensorService = new SensorService(getCtx());
    mServiceIntent = new Intent(getCtx(), mSensorService.getClass());
    if (!isMyServiceRunning(mSensorService.getClass())) {
        startService(mServiceIntent);
    }
}

这是我的服务类

   public class SensorService extends Service{

public int counter=0;
public SensorService(Context applicationContext) {
    super();
    Log.i("HERE", "here I am!");
}

public SensorService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    startTimer();
    return START_STICKY;
}
@Override
public void onDestroy() {
    super.onDestroy();
    Log.i("EXIT", "ondestroy!");
    Intent broadcastIntent = new Intent("uk.ac.shef.oak.ActivityRecognition.RestartSensor");
    sendBroadcast(broadcastIntent);
  }

private Timer timer;
private TimerTask timerTask;
long oldTime=0;
public void startTimer() {
    //set a new Timer
    timer = new Timer();

    //initialize the TimerTask's job
    initializeTimerTask();

    //schedule the timer, to wake up every 1 second
    timer.schedule(timerTask, 1000, 1000); //
}

/**
 * it sets the timer to print the counter every x seconds
 */
public void initializeTimerTask() {
    timerTask = new TimerTask() {
        public void run() {
            Log.i("in timer", "in timer ++++  "+ (counter++));
        }
    };
}

/**
 * not needed
 */


@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

}

推荐答案

说来话长.我已经经历过了.还是实现了.现在我的服务在每个 boot_complete 事件上运行,并且一直在运行(带有通知).

Its a long story. I have gone through it. Still implemented it. Now my service runs on every boot_complete event and keeps running all the time ( with a notification ).

  • 官方文档:
  • 大号.Google android 开发人员文档很差,也没有适当的示例示例.这是理论上的,只是理论上的.有兴趣请继续阅读

    Big NO. Google android developer documentation is poor, with no proper sample example too. It is theoretical and just theoretical. Keep reading if interested

    https://developer.android.com/about/versions/oreo/background

    概要 1:您只能接收 BOOT_COMPLETE 并且在传统接收器中只能接收少量广播.休息所有需要在服务中实现运行时的广播接收器,方法是通过始终运行的服务中的代码注册它们.

    Synopsis 1: You can only receive BOOT_COMPLETE and only few broadcasts in traditional receiver. Rest all broadcast receivers you need to implement runtime in a service by registering them through a code from service which always runs.

    概要 2:同样,您不能总是在 8.0 (Oreo) 或更高版本中运行进程...为了实现始终运行的进程...创建一个 Intentservice 并使用 ongoing 类型的适当通知并制作 OnStartCommand START_STICKY 和使用 OnCreate

    Synopsis 2: Again, you can not have always running processes in or above 8.0 (Oreo)... To achieve always running process... Create a Intentservice with proper notification of type ongoing and make OnStartCommand START_STICKY and register receiver with code in OnCreate

    如何实现:我已经实现了它从这里参考:Oreo:广播接收器不工作

    How to implement it : I have implemented it take reference from here : Oreo: Broadcast receiver Not working

    现在你的问题:我想在后台运行我的应用程序,如果它杀死应用实例.

    Now Your Question : I want to run my app in background if it kills the app instance also.

    借助上面我自己的实现链接你就可以实现了

    With the help of above implementation link of my own you can achieve it

    *条款和条件

    您的设备必须有适当的 Android 操作系统,并按原样烧录.

    You device must have proper android operating system released and burnt as it is.

    是的,我使用的是安卓:

    Yes, I am using android :

    No... You are Using Funtouch OS : VIVO ( By modifying Android)
    

    市面上有很多设备 COLOR OS : OPPO (通过修改安卓)……....

    There are many devices in market COLOR OS : OPPO ( By modifying Android) .... ....

    1. 谷歌已经把它变得复杂了……逐个版本……
    2. 没有适当的文档和示例代码....
    3. 现在独立的移动设备制造商制造了大量更改只允许选择性应用程序在后台运行喜欢 WhatsAppFacebookGoogle Twitter Instagram
    1. Already google has made it complicated... version by version....
    2. With no proper documentation and sample codes....
    3. And Now Independent mobile device manufacturers making a lot of changes to allow only selective applications run in background like WhatsApp, Facebook, Google Twitter Instagram

    现在你会问一个开发者问题如果这些应用程序在后台运行,那么我也可以让我的应用程序在后台运行....

    Now you will ask a developer question If these app runs in background then I can make my app run in background too....

    不...它们是基于操作系统的修改,用于检查服务是否来自允许的供应商,然后只有它可以在后台存在.如果他们不允许这些供应商,那么没有人会拿走不运行这些著名社交应用程序的手机.

    No... They are OS based modifications to check if a service is from allowed vendors then only it can be alive there in background. If they will not allow these vendors then no one take phones which does not run these famous social apps.

    嘘…………

    这篇关于后台服务在奥利奥中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆