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

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

问题描述

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

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 并正确通知进行中类型,并执行 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:广播接收器不工作


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

    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


    *条款和条件

    *Terms and conditions

    您的设备必须已发布并正确刻录了正确的android操作系统。

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


    是的,我正在使用android:

    Yes, I am using android :



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

    市场上有很多设备是彩色OS:OPPO(通过修改Android)
    ....
    ....

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


    1. 谷歌已经使它变得复杂了……逐个版本....

    2. 没有适当的文档和示例代码...。

    3. 现在,独立的移动设备制造商对
      进行了大量更改以便仅允许在后台运行选择性应用程序
      ,例如 WhatsApp Facebook Google 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.

    Hushhhhhhhh .......

    Hushhhhhhhh.......

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

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