应用关闭后,AlarmManager无法正常工作吗? -安卓 [英] AlarmManager is not working after app is closed? - Android

查看:88
本文介绍了应用关闭后,AlarmManager无法正常工作吗? -安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Alarmmanager有一个小问题,但找不到适合我代码的答案。

I have a small problem about alarmmanager but I couldn't find a answer which fits to my code.

我的问题很简单。我有一个设置为将来的警报列表。当我的应用程序运行时,我可以收到通知。

My question is simple. I have a list of alarms which is set to future. While my app is running, I can recieve Notification.

但是当我关闭我的应用程序时,它不会向我发送通知,并且如果我再次运行我的应用程序,过去通知可以在通知中心看到。

But when I close my app, It doesn't send notification to me and if I run my app again, past notifications can be seen in notification center.

这是我的代码。
在MainActivity.java中,我使用此方法可以获取人员列表并设置每个人员的警报。我在onCreate()中运行此方法

Here is my codes. In MainActivity.java I use this method which can take a Person List and sets alarm of each of Person. I run this method in onCreate()

private void createScheduledNotification(List<Person> people)
{
    for(int i = 0; i<people.size();i++) {

        // Get new calendar object
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, people.get(i).getMonth());
        calendar.set(Calendar.DAY_OF_MONTH, people.get(i).getDay());
        calendar.set(Calendar.YEAR, Calendar.getInstance().get(Calendar.YEAR));

        calendar.set(Calendar.HOUR_OF_DAY, people.get(i).getHour());
        calendar.set(Calendar.MINUTE, people.get(i).getMinute());
        calendar.set(Calendar.SECOND, 0);

        // Retrieve alarm manager from the system
        AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(getBaseContext().ALARM_SERVICE);
        // Every scheduled intent needs a different ID, else it is just executed once
        int id = (int) System.currentTimeMillis();

        // Prepare the intent which should be launched at the date
        Intent intent = new Intent(this, TimeAlarm.class);
        intent.putExtra("person", people.get(i));
        // Prepare the pending intent
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        // Register the alert in the system. You have the option to define if the device has to wake up on the alert or not
        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    }
}

我有一个TimeAlarm类,它扩展了BroadcastReciever。 / p>

And I have a TimeAlarm class which extends BroadcastReciever.

public class TimeAlarm extends BroadcastReceiver {

private Person person;

@Override
public void onReceive(Context context, Intent paramIntent) {

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

我也将这些行添加到AndroidManifest.xml

Also I add these lines to AndroidManifest.xml

        <receiver android:name=".TimeAlarm" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

在应用程序运行时工作正常,但是当我在活动应用程序页面中关闭我的应用程序时,它不会t发送任何通知。

It works fine when app is running but when I close my app in Active Applications page, it doesn't send any notification.

推荐答案

在创建意图时尝试使用此功能

Try use this when you create your intent

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

然后在BroadcastReciver中检查该操作是否为启动完成操作。 (也可能是您的警报)。

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

也请检查

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?

编辑:

对不起,我不记得要插入它了,您的收货人声明应该是

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>

这篇关于应用关闭后,AlarmManager无法正常工作吗? -安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆