应用程序上的广播接收器关闭(或在后台) [英] Broadcast receiver on app closed (or in the background)
问题描述
我创建了一个简单的警报应用,我在 AndroidManifest.xml
中注册了两个广播接收器
(一个用于接收警报和其他警报,以便在设备启动时重新安排它们)。安排了一些警报后,我重新启动了设备,我的 OnBoot接收器
重新安排了警报,但是第一个接收器无法接收到警报。如果我启动我的应用程序,安排一些警报并且不关闭它,则接收器将正常工作,但是如果我关闭(而不是强制停止)应用程序或通过按主按钮将其发送到后台
,接收方将永远不会收到警报!我在几种设备(HTC,魅族,三星)上进行了测试,但遇到了同样的问题。我发现使接收器正常工作的唯一方法是杀死应用程序,因为再次启动它后,即使应用程序已关闭,接收器也能正常工作,但是再次重启设备后,我也会遇到同样的问题。拜托,有人可以帮我吗?我需要做些什么来摆脱这个问题?
I create a simple alarms app and I have two broadcast receivers
registered in AndroidManifest.xml
(One for receiving alarms and other for rescheduling them on device boot). After scheduling some alarms I reboot my device and my "OnBoot" receiver
rescheduling alarms well, but the first receiver doesn't recive them. If I start my app, schedule some alarms and won't close it, the receiver will work fine, but if I close (not force stop) app or send it into background by pressing the home button
, receiver will never receive alarms! I tested it on several devices (HTC, Meizu, Samsung) and got same problem. There is only one way I found to make the receiver work normally is to kill app, because after I start it again, the receiver is working normally even app is closed, but after I reboot device again I will get same problem. Please, can anyone help me with that? What I need to do to get rid of this problem?
我几乎尝试了所有操作(更改 pendingintent标志
和请求
代码,正在运行的接收器
在远程进程
中,设置意图过滤器
,扩展
(来自 WakefulBroadcastReceiver
等),但没有任何帮助。
I tried almost everything(changing pendingintent flags
and request
codes, running receivers
in remote processes
, setting an intent-filters
, extend
from WakefulBroadcastReceiver
and many other) but nothing has helped me.
AndroidManifest.xml中的接收器
<receiver
android:name="com.gd.aiwnext.deal.Receivers.NotificationsReceiver"
android:enabled="true"
android:exported="true"
android:process=":nr">
<intent-filter>
<action android:name="com.gd.action.intent.bri"></action>
</intent-filter>
</receiver>
<receiver
android:name="com.gd.aiwnext.deal.Receivers.NotificationBootReceiver"
android:process=":nbr">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
BroadcastReceiver
public class NotificationsReceiver extends WakefulBroadcastReceiver {
SQLiteDatabase NoteDataBase;
NotesDataBase notesDataBase;
String OLDDATE, NEWDATE, INTPICKER, OLDTIME, NEWTIME;
String NewYear, NewMonth, NewDay, NewHour, NewMinute;
long CurrentTimeInMillis, FutureTimeInMillis;
Boolean StartAfterSleep;
String FinalHour = "", FinalMinute = "", FinalYear = "", FinalMonth = "", FinalDay = "";
boolean isDivided = false;
@Override
public void onReceive(Context context, Intent intent) {
Log.d("BLL","Simple");
int id = intent.getIntExtra("id", 1);
String note = intent.getStringExtra("note");
String interval = intent.getStringExtra("interval");
OLDDATE = intent.getStringExtra("date");
OLDTIME = intent.getStringExtra("time");
INTPICKER = intent.getStringExtra("intpicker");
CurrentTimeInMillis = intent.getLongExtra("timeinmillis", 0);
if (intent.hasExtra("startaftersleep")) {
StartAfterSleep = (intent.getBooleanExtra("startaftersleep", false));
} else {
StartAfterSleep = false;
}
CreateNotification(id, note, interval, context);
}
我使用CreateNotification()方法发送通知和数据库以更新时间和日期
I use CreateNotification() method to send notifications and database to update time and date of alarm.
创建警报
Intent intent = new Intent("com.gd.action.intent.bri");
在那之后,我又放了一些东西,然后
After that I put some extra, and
PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(), IdCount, intent, PendingIntent.FLAG_ONE_SHOT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, FutureTimeMillis, pendingIntent);
推荐答案
您应该应用默认类别
<category android:name="android.intent.category.DEFAULT" />
如果未指定类别,则Android将该类别分配给每个意图。
Android assign this category to each intent if category isn't specified.
这篇关于应用程序上的广播接收器关闭(或在后台)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!