由于 Android 团队在“此处”发布了新的后台服务限制,因此通过在 Android O 中的 Widget(主屏幕)按钮上设置单击 Pending Intent 来启动 Intent 服务时出现问题。我的问题是:如何添加新的通过单击小部件按钮将任务发送到 JobScheduler?
我正在使用Evernotes android-job库进行Scheduling作业,一切正常但是,当我想运行一个运行Robolecteric的测试用例时,我得到了这个错误:
com.evernote.android.job.JobManagerCreateException: All APIs are disabled, cannot schedule any job
at com.evernote.android.job.JobManager.<init>(JobManager.java:184)
at com.evernote.android.job.JobManager.create(JobManager.java:107)
at com.M.MyApp.MyApplication.onCreate(MyApplication.java:63)
at org.robolectric.android.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:137)
at org.robolectric.RobolectricTestRunner.beforeTest(RobolectricTestRunner.java:290)
at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:203)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:109)
Run Code Online (Sandbox Code Playgroud)
基本上它指向我的应用程序类的第63行:
JobManager.create(getApplicationContext()).addJobCreator(new JobMaker());
Run Code Online (Sandbox Code Playgroud) 随着Oreo中引入的新后台服务限制,我很难找到一个地方来澄清在后台运行应用程序时是否仍然可以从JobScheduler启动IntentServices。
我有一个基于地理围栏的非常重要的功能,该功能需要在篱笆断开后运行intentService。我花了很长时间设置它,以便在篱笆破损且应用程序处于“牛轧糖”的“打for”状态时排队等待工作。当重新打开网络连接窗口时,作业最终由操作系统运行时,我在intentService中一个接一个地剥离每个作业。从技术上来说,当应用程序不在前台时正在启动服务,或者由于使用JobScheduler而仍然允许这样做时,我现在可以不再执行此操作吗?
android android-intentservice android-jobscheduler android-8.0-oreo
我想每天在我的应用程序中运行一个任务,我正在使用 JobScheduler 并且在股票操作系统中运行良好,但是当我尝试在具有自定义 ROM(小米)的手机中运行它时它不起作用,除非我明确启用自动- 安全中应用程序的启动选项。有没有解决方案,其他应用程序如何处理这种情况?
android alarmmanager android-jobscheduler android-workmanager
我刚刚开发了一个 Android 应用程序(minSdkVersion 23/targetSdkVersion 29),它可以连接到 BluetoothLE 设备以定期获取数据。
现在,在 MainActivity(不是第一个活动)中,我执行以下注册广播接收器:
public class StatusActivity extends AppCompatActivity {
BleService mBleService;
BleScanCallback mScanCallback;
BroadcastReceiver mBroadcastReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
mBroadcastReceiver = new LibBleBroadcastReceiver(this);
IntentFilter intentFilter = new IntentFilter()
intentFilter.addAction(BleService.ACTION_GATT_CONNECTED);
intentFilter.addAction(BleService.ACTION_GATT_DISCONNECTED);
intentFilter.addAction(BleService.ACTION_GATT_SERVICES_DISCOVERED);
intentFilter.addAction(BleService.ACTION_DATA_AVAILABLE);
intentFilter.addAction(BleService.ACTION_DID_WRITE_CHARACTERISTIC);
intentFilter.addAction(BleService.ACTION_DID_FAIL);
registerReceiver(mBroadcastReceiver, intentFilter);
mScanCallback = new LibBleScanCallback(this);
intent = new Intent(this, BleService.class);
connection = new LibBleServiceConnection(this);
startService(intent);
if (!bindService(intent, connection, Context.BIND_AUTO_CREATE)) {
throw new IllegalStateException("bindService not successful");
}
}
...
public void onDeviceDiscovered(String device_address){
device_connected.activateNotifications(mBleService, connected_device);
scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() { …Run Code Online (Sandbox Code Playgroud) android background-process android-bluetooth android-jobscheduler android-workmanager
我有一个service扩展JobService如下:
public class MyService extends JobService {
private int notificationNumber = 0;
private Thread thread;
@Override
public boolean onStartJob(JobParameters params) {
Toast.makeText(this, "Service1 Started with JobId: " + params.getJobId(), Toast.LENGTH_LONG).show();
thread = new WorkerThread(this, params);
thread.start();
return true;
}
@Override
public boolean onStopJob(JobParameters params) {
createNotification("Service1 onStop with JobId");
return false;
}
public void createNotification(String msg) {
notificationNumber++;
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentText(msg);
builder.setContentTitle("New Notification #" + notificationNumber);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); …Run Code Online (Sandbox Code Playgroud) 我的应用程序安排JobScheduler,周期间隔为60秒:
val service = ComponentName(packageName, MonitorService::class.java.name)
val job = JobInfo.Builder(JOB_ID, service)
.setPeriodic(interval) // interval is 60*1000
.setPersisted(true)
.setRequiresCharging(false)
.build()
jobScheduler.schedule(job)
Run Code Online (Sandbox Code Playgroud)
我知道Android操作系统优化了作业调度程序的事实,因此它永远不会完全安排,但需要2~3分钟后.我在自己的设备上验证了这种行为(HTC One M8 with Android 6.0).
在许多(各种)的其它设备在该间隔是多少更不频繁(如一次/两次每小时仅).
我该如何解决这个问题?Job Scheduler可能不是我的用例的正确API吗?
我正在尝试使用 安排任务在特定时间运行WorkManager。我之所以使用它beginUniqueWork,是因为我只想为该特定 ID (uniqueWorkName) 一次安排一项任务。但enqueue多次调用后,有时会出现以下错误:
java.lang.IllegalStateException: Apps may not schedule more than 100 distinct jobs
at android.os.Parcel.readException(Parcel.java:2012)
at android.os.Parcel.readException(Parcel.java:1950)
at android.app.job.IJobScheduler$Stub$Proxy.schedule(IJobScheduler.java:180)
at android.app.JobSchedulerImpl.schedule(JobSchedulerImpl.java:44)
at androidx.work.impl.background.systemjob.SystemJobScheduler.scheduleInternal(SystemJobScheduler.java:85)
at androidx.work.impl.background.systemjob.SystemJobScheduler.schedule(SystemJobScheduler.java:64)
at androidx.work.impl.Schedulers.scheduleInternal(Schedulers.java:98)
at androidx.work.impl.Schedulers.schedule(Schedulers.java:69)
at androidx.work.impl.WorkManagerImpl.rescheduleEligibleWork(WorkManagerImpl.java:398)
at androidx.work.impl.utils.ForceStopRunnable.run(ForceStopRunnable.java:66)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Run Code Online (Sandbox Code Playgroud)
当我在 every 之后使用以下代码片段记录待处理作业的数量时enqueue,我注意到每次调用都会有 3 个新作业添加到列表中(虽然我预计总数会保持在 1)。
JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
int size = jobScheduler.getAllPendingJobs().size();
Run Code Online (Sandbox Code Playgroud)
这是我用来安排任务的代码:
val work = OneTimeWorkRequest.Builder(workerClass)
.setInitialDelay(offset, TimeUnit.MILLISECONDS)
.build()
WorkManager.getInstance()
.beginUniqueWork(uniqueNameForTask, ExistingWorkPolicy.REPLACE, work)
.enqueue()
Run Code Online (Sandbox Code Playgroud)
这里看起来有什么不对劲吗?我缺少什么?
android android-jobscheduler android-jetpack android-workmanager
大家好,我正在尝试在 Android 上实现定期任务,但我被困在某些设备上。
我需要每 15 或 30 分钟在后台运行一次任务。这在 Android 8.0 之前的版本上运行良好。但在 8+ 上,它仅在应用程序处于后台或前台时才有效。当应用程序从最近的应用程序中滑出时,计划任务在真实设备(Ulefone note 7(Android 8.1)、Tecno LC7(Android 10)、itel A56(Abdroid 9))上被终止,但在模拟器(Android 10)上运行良好。我尝试了几种方法:
1.Workmanager(仅当应用程序处于后台或前台时才工作)
构建.gradle
implementation "androidx.work:work-runtime:2.4.0"
Run Code Online (Sandbox Code Playgroud)
主要活动
PeriodicWorkRequest periodicSyncDataWork =
new PeriodicWorkRequest.Builder(NotificationWorker.class, 15,TimeUnit.MINUTES)
.addTag("TAG_SYNC_DATA")
.setBackoffCriteria(BackoffPolicy.LINEAR,PeriodicWorkRequest.MIN_BACKOFF_MILLIS, TimeUnit.MILLISECONDS)
.build();
WorkManager.getInstance(this).enqueueUniquePeriodicWork(
"NOTIFICATION_WORKER",
ExistingPeriodicWorkPolicy.REPLACE, //Existing Periodic Work policy
periodicSyncDataWork //work request
);
Run Code Online (Sandbox Code Playgroud)
通知工作者
public class NotificationWorker extends Worker {
public NotificationWorker(@NonNull Context context, @NonNull WorkerParameters workerParams)
{
super(context, workerParams);
}
@NonNull
@Override
public Result doWork() {
Log.d("MYWORKER", "LLLLLLLLLLL");
//My code here
return Result.success();
}
Run Code Online (Sandbox Code Playgroud)
} …
android alarmmanager periodic-task android-jobscheduler android-workmanager
我每天晚上必须从Web服务器上下载json响应,以前我曾经用于AlarmManager计划任务,但是我认为这种情况JobDispatcher非常好,因为如果网络可用,它会自动执行任务,因此我不必管理这种stuf 。但是我发现了很多例子,在一个例子中JobDispatcher,JobScheduler一个简单的工作被安排或计划了一段时间的延迟,但是与我的要求没有任何关系,如果有人对此有所了解,请帮助或提供与此有关的任何链接,将非常有帮助。
更新
1. 如何使它每天晚上工作,因为当前仅将闹钟设置为午夜一次,如何使其每天晚上重复一次?
android alarmmanager android-jobscheduler firebase-job-dispatcher
我有一个连接到Bitmessage网络的应用程序,即一直从P2P网络下载和处理数据.此外,它应该(可选)仅使用WiFi /未计量网络.
我已经通过使用JobScheduler实现了这一点,但不幸的是它超时了10分钟(在Lollipop上显然甚至是1分钟).
简而言之,我该如何实现一项服务呢
在build.gradle我的项目上进行配置
compileSdkVersion 27
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.android.tt"
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
multiDexEnabled true
generatedDensities = []
}
Run Code Online (Sandbox Code Playgroud)
和以下支持库,我已经编译
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:design:25.1.0'
compile 'com.android.support:cardview-v7:25.1.0'
compile 'com.android.support:support-v4:25.1.0'
compile 'com.android.support:recyclerview-v7:25.1.0'
Run Code Online (Sandbox Code Playgroud)
当我尝试导入JobIntentService类时,它无法解决。下面的链接我正在尝试实现
我正在尝试启动一个IntentService来注册Android O上的firebase云消息.
在Android O上,不允许"在不允许的情况下"启动Intent Service,并且每个人都告诉我使用JobService而不是如何使用它.
JobInfo.Builder应该有什么限制才能有"允许它的情况",我一直得到相同的IllegalStateException
这是我的JobService
@Override
public boolean onStartJob(JobParameters params) {
Intent intent = new Intent(this, RegistrationIntentService.class);
getApplicationContext().startService(intent);
return false;
}
@Override
public boolean onStopJob(JobParameters params) {
return false;
}
public static void scheduleJob(Context context) {
ComponentName serviceComponent = new ComponentName(context, MyJobService.class);
JobInfo.Builder builder = new JobInfo.Builder(MyJobService.JOB_ID, serviceComponent);
builder.setMinimumLatency(1 * 1000); // wait at least
JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
if(jobScheduler != null) jobScheduler.schedule(builder.build());
}
Run Code Online (Sandbox Code Playgroud)