4

从文档中不清楚应用程序startService()是否可以在收到意图后处于空闲状态时调用。文档讨论了短信/彩信广播意图,如果应用程序通过广播接收器接收到任何意图,则不清楚该应用程序是否被置于白名单中。直到现在我还没有找到一种方法来测试它,让应用程序处于空闲状态。有小费吗?

4

2 回答 2

0

从 Android O 开始,您只能在以下情况下调用 startService():

  1. 您的应用程序在前台。
  2. 您使用 JobScheduler/JobService 来包装调用。

对于#2,您基本上替换:

startService(startMyServiceIntent);

有了这个:

ComponentName serviceName          = new ComponentName(context, MyJobService.class);
JobScheduler  jobScheduler          = (JobScheduler)context.getSystemService(Context.JOB_SCHDULER_SERVICE);
JobInfo       startMySerivceJobInfo = new JobInfo.Builder(MyJobService.JOB_ID, serviceName).setMinimumLatency(100).build();
int           result                = jobScheduler.schedule(startMyServiceJobInfo);

然后你只需要一个扩展 JobService 的类来实际启动服务:

public class MyJobService extends JobService {
  public static final int JOB_ID = 0;  // app unique job id. 

  @Override
  public boolean onJobStart(JobParameters params) {
    ...
    startService(startMyServiceIntent);
    ...
    return false;
  }
}

这将在前台或后台启动您的服务。后台服务可以运行多长时间仍然存在后台限制。看到这个:https ://stackoverflow.com/a/44241192/786462

于 2017-06-01T00:19:01.857 回答
-1

我回复自己:不,只有一些系统意图将应用程序列入白名单

于 2018-05-16T18:25:54.123 回答