后台服务在奥利奥中不起作用

gow*_*ami 1 android background android-8.1-oreo

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

我按照此链接在后台运行,但如果我删除实例,它就不起作用。如果实例也被删除,请任何人告诉我如何运行后台服务?

这是我的主要活动

@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);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的服务类

   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;
}
Run Code Online (Sandbox Code Playgroud)

}

san*_*ane 5

这是一个很长的故事。我已经经历过了。还是执行了。现在我的服务在每个 boot_complete 事件上运行并一直运行(带有通知)。

  • 官方文档:

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

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

概要1:BOOT_COMPLETE在传统接收器中, 您只能接收并且只有很少的广播。通过来自始终运行的服务的代码注册它们,从而使您需要在服务中实现运行时的所有广播接收器休息。

概要 2: 同样,您不能始终在 8.0 (Oreo) 或更高版本中运行进程......要实现始终运行的进程......创建一个Intentservice具有正确类型通知并使用代码ongoing制作OnStartCommand START_STICKY和注册接收器OnCreate

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

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

借助我自己的上述实现链接,您可以实现它

*条款和条件

您的设备必须有适当的 android 操作系统发布并按原样烧毁。

是的,我正在使用 android :

No... You are Using Funtouch OS : VIVO ( By modifying Android)
Run Code Online (Sandbox Code Playgroud)

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

  1. 谷歌已经让它变得复杂了......一个版本一个版本......
  2. 没有适当的文档和示例代码......
  3. 而现在独立的移动设备制造商制造了很大的变化to allow only selective applications run in background 一样WhatsAppFacebookGoogle Twitter Instagram

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

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

嘘…………