【问题标题】:AlarmManager is not working after app is closed? - Android应用程序关闭后AlarmManager 不工作? - 安卓
【发布时间】:2016-05-25 20:23:17
【问题描述】:

我有一个关于警报管理器的小问题,但我找不到适合我的代码的答案。

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

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

这是我的代码。 在 MainActivity.java 我使用这个方法,它可以获取一个人员列表并设置每个人员的警报。我在 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);
    }
}

我有一个扩展 BroadcastReciever 的 TimeAlarm 类。

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

        <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" />

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

【问题讨论】:

    标签: android broadcastreceiver alarmmanager


    【解决方案1】:

    在创建意图时尝试使用它

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

    并在您的 BroadcastReciver 中检查该操作是否为启动完成操作。 (也可能是你的闹钟)。

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

    因为您似乎在不断更新相同的意图,这是您想要的吗?

    编辑:

    对不起,我不记得插入它,你的收件人声明应该是这样的。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-07
      • 2012-04-16
      • 2012-06-02
      • 1970-01-01
      相关资源
      最近更新 更多