AlarmManager is not working after app is closed? - Android

拟墨画扇 提交于 2019-12-06 15:41:15

Try use this when you create your intent

Bundle extras = new Bundle();
extras.putSerializable("person", people.get(i));
intent.putExtras(extras);       

And in your BroadcastReciver check if the action is a boot completed action. (it could also be your alarm).

@Override
public void onReceive(Context context, Intent paramIntent) {
                     //CHECK IF IS BOOT COPLETED 
                 if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
                            /* Setting the alarm here Alarm are automatically cleaned on phone shutdown*/}
                 else{
                        Bundle bundle = paramIntent.getExtras();
                        person = (Person) bundle.getSerializable("person");
                        // Request the notification manager
                        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

                        // Create a new intent which will be fired if you click on the notification
                        Intent intent = new Intent("android.intent.action.VIEW");

                        // Attach the intent to a pending intent
                        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

                        // Create the notification
                        Notification notification = new Notification(R.drawable.logo24px, "Never Forget", System.currentTimeMillis());
                        //
                        notification.setLatestEventInfo(context, "It's " + person.getName() + " " + person.getSname() + "'s Birthday!", "Celebrate his/her birthday! ",pendingIntent);

                        // Fire the notification
                        notificationManager.notify(1, notification);}
                    }
    }

Also check this

PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

because it seems that you are continusly updating the same intent, is what you want?

Edit:

Sorry I didn't remember to insert it, your reciever declaration sholuld be like this.

<receiver android:name=".TimeAlarm" 
        android:process=":remote"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!