Broadcast receiver on app closed (or in the background)

心已入冬 提交于 2019-12-11 04:44:56

问题


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?

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.

Receivers in 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);
}

I use CreateNotification() method to send notifications and database to update time and date of alarm.

Creating alarms

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);

回答1:


You should apply default category

<category android:name="android.intent.category.DEFAULT" />

Android assign this category to each intent if category isn't specified.



来源:https://stackoverflow.com/questions/37110674/broadcast-receiver-on-app-closed-or-in-the-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!