在Android中创建定时通知(例如,用于事件)

caw*_*caw 5 notifications android alarmmanager

对于某些Android应用程序,我想集成以下功能:用户可以定义他想要被提醒的时间.当时间到了,应用程序应该在通知栏中创建通知,即使此时用户没有使用该应用程序.

为此,需要查看AlarmManager,NotificationManager和Notification.Builder类,对吧?

那么我该如何提前创建定时通知呢?我的代码(到目前为止)是这样的:

将其添加到AndroidManifest下以注册广播接收器:

<receiver android:name="AlarmNotificationReceiver"></receiver>
Run Code Online (Sandbox Code Playgroud)

创建一个新的类文件来处理它收到的警报:

public class AlarmNotificationReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        Bundle extras = intent.getExtras();
        if (extras != null) {
            String additionalData = extras.getString("displayText");
            // show the notification now
            NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            Notification mNotification = new Notification(R.drawable.ic_launcher, context.getString(R.string.app_name), System.currentTimeMillis());
            PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0); // open MainActivity if the user selects this notification
            mNotification.setLatestEventInfo(context, context.getString(R.string.app_name), additionalData, pi);
            mNotification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_SOUND;
            mNotificationManager.notify(1, mNotification);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

使用此代码(例如在MainActivity中)将警报设置为3秒后:

    Intent i = new Intent(this, AlarmNotificationReceiver.class);
    i.putExtra("displayText", "sample text");
    PendingIntent pi = PendingIntent.getBroadcast(this.getApplicationContext(), 234324246, i, 0);
    AlarmManager mAlarm = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    mAlarm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+3*1000, pi);
Run Code Online (Sandbox Code Playgroud)

为了使这项工作,我需要改变什么?谢谢!

这两个问题是:

  1. 当我在代码中更改通知时,通知的文本不会更改.它只会在我更改PendingIntent.getBroadcast(...)中的requestCode时更改.这个请求代码到底是什么?可以是随机值还是0?
  2. 重新启动手机后,"计划"通知或警报消失.但是现在我已经看到这是正常行为,对吗?我怎么能绕过这个呢?

dan*_*h32 4

不确定第 1 部分,但对于第 2 部分,一般方法是拦截 BOOT_COMPLETED 意图并使用它来重新注册所有警报。不幸的是,这意味着对于您在警报管理器中注册的每个警报,您也必须将其存储在应用程序的数据库中。

因此,您需要一个广播接收器来拦截 BOOT_COMPLETED 意图:

public class BootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // get your stored alarms from your database
        // reregister them with the alarm manager
    }
}
Run Code Online (Sandbox Code Playgroud)

要获得 BOOT_COMPLETED 意图,您必须在清单中添加以下权限:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Run Code Online (Sandbox Code Playgroud)

并且 BootReceiver 还需要使用以下意图过滤器在清单中注册:

    <receiver android:enabled="true" android:name=".receiver.BootReceiver"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
        <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>
Run Code Online (Sandbox Code Playgroud)

需要注意的是,如果您的应用程序安装到 sdcard,它永远无法接收 BOOT_COMPLETED 意图。另外,值得注意的是,这种实现有点幼稚,因为它在启动时立即执行代码,这可能会减慢用户手机在启动时的速度。因此,我建议在拦截启动意图后延迟执行几分钟。