Network Changed Broadcast Receiver does not execute in One Plus Phones

后端 未结 5 802
不知归路
不知归路 2021-01-21 06:47

I have a BroadcastReciever name NetworkReciver.java that executes when Internet is Connected or Disconnected. And it is working well.

But when

5条回答
  •  借酒劲吻你
    2021-01-21 06:58

    Apps targeting Android 7.0+ do not receive CONNECTIVITY_ACTION broadcasts if they register to receive them in their manifest, and processes that depend on this broadcast will not start.

    So, if you want to do some work when internet connection is available. You can use Job scheduler or work manager.

    For example, here is sample code for job scheduler.

    public static final int MY_BACKGROUND_JOB = 0;
    ...
    public static void scheduleJob(Context context) {
      JobScheduler js =
          (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
      JobInfo job = new JobInfo.Builder(
        MY_BACKGROUND_JOB,
        new ComponentName(context, MyJobService.class))
          .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
          .setRequiresCharging(true)
          .build();
      js.schedule(job);
    }
    

    When the conditions for your job are met, your app receives a callback to run the onStartJob() method in the specified JobService.class

    Android JobScheduler Sample

    Also, registering broadcasts in the activity's onCreate and unregistering it in onDestroy will not work for your case as you will not receive the broadcast after the app is killed.

提交回复
热议问题