无法启动接收器:不允许启动服务意向;应用程序在后台 [英] Unable to start receiver : Not allowed to start service Intent ; App is in background
问题描述
因此,我制作了一个应用程序,只需单击按钮,即可使用警报管理器设置重复任务。
So I made an app that upon a button click sets up a repeating task using an Alarm Manager.
在创建时输入:
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
servicePendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
在按钮上单击:
alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
firingCal= Calendar.getInstance();
firingCal.setTimeInMillis(System.currentTimeMillis());
firingCal.set(Calendar.HOUR_OF_DAY, 1); // At the hour you want to fire the alarm
firingCal.set(Calendar.MINUTE, 47); // alarm minute
firingCal.set(Calendar.SECOND, 0); // and alarm second
long intendedTime = firingCal.getTimeInMillis();
long interval = 1000 * 60 * 1;
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, intendedTime, interval, servicePendingIntent);
在AlarmReceiver类中:
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(context, WallpaperService.class);
context.startService(myIntent);
Log.d(TAG,"Am apelat serviciul");
context.stopService(myIntent);
}
在WallpaperService类中,我只是发出一个get请求,然后设置墙纸。
And in the WallpaperService class I just make a get request and set an wallpaper.
public class WallpaperService extends Service {
String requestLink="";
boolean requestFinished = false;
public final String TAG = "Service";
public static int SERVICE_ID = 1;
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG,"Wallpaper Service started");
Toast.makeText(WallpaperService.this,"Service started",Toast.LENGTH_LONG);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG,"In onStartCommand");
taskToBeRepeated();
stopSelf();
return START_STICKY;
}
.....
}
行为是,当我启动该应用程序并单击该按钮时,警报管理器在第一次启动时一切正常(在后台运行该应用程序)。第二次接收器触发时,我在磁贴中收到错误。更具体地说:
And the behaviour is that when I start the app and I click the button everything works well the first time the Alarm Manager fires ( With the app in the background). The second time the receiver gets triggered I get the error in the tile. To be more specific :
java.lang.RuntimeException:无法启动接收器com.example.dailywallpaper.AlarmReceiver:java.lang.IllegalStateException :不允许启动服务意图{cmp = com.example.dailywallpaper / .WallpaperService}:应用程序在后台uid UidRecord {3e313bf u0a357 RCVR bg:+ 1m21s273ms空闲更改:未处理的进程:1 seq(0,0,0)}
java.lang.RuntimeException: Unable to start receiver com.example.dailywallpaper.AlarmReceiver: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.example.dailywallpaper/.WallpaperService }: app is in background uid UidRecord{3e313bf u0a357 RCVR bg:+1m21s273ms idle change:uncached procs:1 seq(0,0,0)}
似乎是什么问题?为何先工作然后又给出错误?我该如何解决?
What seems to be the problem ? And why is working the first time and then it gives the error? How can I fix it ?
推荐答案
您需要阅读android官方文档,以了解在android 8及更高版本中使用后台服务或警报的策略,并适应您的应用
you need to read android official documentation about the policy of using background service or alarms in android 8 and above and adapt your app with this limitations.
我建议您仔细阅读这两篇文章:
I suggest you to read this two articles very carefully :
https://developer.android.com/guide/components/services
https://developer.android.com/about/versions / oreo / background
这篇关于无法启动接收器:不允许启动服务意向;应用程序在后台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!