在我关闭应用程序或使用WorkManager、AlarmManager或Jobscheduler重新启动后,java任务永远不会执行
我想要实现的事情:
- 无论应用程序被关闭或设备重新启动,任务都将在后台持续运行。比方说,我正在将联系人同步到服务器,我需要每分钟检查是否有新联系人,这对我的任务非常重要李>
- 如果我们进入开发者设置,然后“运行服务”,这些服务永远不会被杀死。并在设备重新启动后继续重新启动。例如
facebookServices
或WhatsApp
李>
我正在使用的物理设备的信息:
Android 10.
API Level 29
我尝试过的事情:
JobScheduler
WorkManager
Service (startService())
JobIntentService
AlarmManager
我面临的问题:
我读了很多文章、博客,看了很多视频,但仍然无法实现我真正想要的。我也知道如果我使用带有通知通道的foregroundService,我的服务将不会被终止并持续,但我不希望这样,因为FaceBooks服务必须在没有任何通知通道的情况下运行,它们只是在运行后台服务,即使在API级别29上,在Android O之后有所有后台限制
- 将AlarmManager与BroadcastReceiver结合使用,尝试在特定时间启动服务。但是有持续的异常抛出
java.lang.IllegalStateException: Not allowed to start service Intent
李> - 尝试过workManager,它只在应用程序打开时工作,而不是在我关闭应用程序时。如果我重新启动设备,它也不会重新启动工作李>
- JobScheduler也无法准确获取我想要的内容李>
AndroidManifest。xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
package="com.example.basicplayer">
<uses-permission 安卓:name="安卓.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission 安卓:name="安卓.permission.WAKE_LOCK"/>
<application
安卓:allowBackup="true"
安卓:icon="@mipmap/ic_launcher"
安卓:label="@string/app_name"
安卓:roundIcon="@mipmap/ic_launcher_round"
安卓:supportsRtl="true"
安卓:theme="@style/AppTheme">
<activity 安卓:name=".MainActivity">
<intent-filter>
<action 安卓:name="安卓.intent.action.MAIN" />
<category 安卓:name="安卓.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service 安卓:name=".MyAlarmService"
安卓:enabled="true"
安卓:exported="true"
/>
<receiver 安卓:name=".MyAlarmReceiver"
安卓:enabled="true"
安卓:exported="true"
>
<intent-filter>
<action 安卓:name="安卓.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
我的闹钟接收器。爪哇
package com.example.basicplayer;
import 安卓.content.BroadcastReceiver;
import 安卓.content.ComponentName;
import 安卓.content.Context;
import 安卓.content.Intent;
import 安卓.media.MediaPlayer;
import 安卓.os.Build;
import 安卓.provider.Settings;
import 安卓x.annotation.RequiresApi;
public class MyAlarmReceiver extends BroadcastReceiver {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, MyAlarmService.class));
}
}
我的报警服务。爪哇
package com.example.basicplayer;
import 安卓.app.Notification;
import 安卓.app.Service;
import 安卓.content.Intent;
import 安卓.media.MediaPlayer;
import 安卓.os.IBinder;
import 安卓.provider.Settings;
import 安卓x.annotation.Nullable;
public class MyAlarmService extends Service {
private MediaPlayer mediaPlayer;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mediaPlayer = MediaPlayer.create(this,
Settings.System.DEFAULT_ALARM_ALERT_URI);
mediaPlayer.setLooping(true);
mediaPlayer.start();
return START_STICKY;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
try{
startService(new Intent(this, MyAlarmService.class));
}catch (Exception e){
e.printStackTrace();
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
主要活动。爪哇
package com.example.basicplayer;
import 安卓x.appcompat.app.AppCompatActivity;
import 安卓.app.AlarmManager;
import 安卓.app.PendingIntent;
import 安卓.content.Context;
import 安卓.content.Intent;
import 安卓.media.MediaPlayer;
import 安卓.os.Bundle;
import 安卓.provider.Settings;
import 安卓.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setAlarm();
}
private void setAlarm() {
//getting the alarm manager
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
//creating a new intent specifying the broadcast receiver
Intent i = new Intent(this, MyAlarmReceiver.class);
//creating a pending intent using the intent
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
//setting the repeating alarm that will be fired every day
am.setRepeating(AlarmManager.RTC, 10000, AlarmManager.INTERVAL_FIFTEEN_MINUTES, pi);
Toast.makeText(this, "Alarm is set", Toast.LENGTH_SHORT).show();
}
}
你们能帮忙吗
# 1 楼答案
在新版安卓系统中,对后台运行的进程进行了限制。他们这样做的原因是为了优化电池。我认为你应该使用workmanager库。这是一个很棒的后台处理库。此外,当设备重新启动时,您可以使用workmanager启动作业。我为你找到了答案:PeriodicWorkRequest not working after device reboot in android oreo