[英]Android - Create Repeating Notification At Set Time
I have looked through a ton of documentation and stackoverflow questions on how to set a repeating notification set to a certain time but can not get any of them to work. 我浏览了许多文档和stackoverflow问题,以了解如何将重复通知集设置为特定时间,但无法使它们中的任何一个起作用。 Here is what I have right now. 这就是我现在所拥有的。
The method I set up the AlarmManager in: 我在以下位置设置AlarmManager的方法:
//set alarm method
private void setAlarm() {
if(enableCheckBox.isChecked()) {
//save time / title / message
mTinyDB.putInt(Constants.SAVED_HOUR, timePicker.getCurrentHour());
mTinyDB.putInt(Constants.SAVED_MINUTE, timePicker.getCurrentMinute());
mTinyDB.putString(Constants.SAVED_TITLE, titleEditText.getText().toString().trim());
mTinyDB.putString(Constants.SAVED_MESSAGE, messageEditText.getText().toString().trim());
mTinyDB.putBoolean(Constants.SAVED_NOTIFICATION_ENABLED, enableCheckBox.isChecked());
//create repeating notification
Intent intent = new Intent(NotificationActivity.this, Notify.class);
AlarmManager manager = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(this,
0, intent, 0);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, mTinyDB.getInt(Constants.SAVED_HOUR, 0));
cal.set(Calendar.MINUTE, mTinyDB.getInt(Constants.SAVED_MINUTE, 0));
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
manager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent);
}
}
The Notify class which extends BoradcastReceiver: 扩展BoradcastReceiver的Notify类:
public class Notify extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
TinyDB mTinyDB = new TinyDB(context);
Notification builder = new Notification.Builder(context)
.setContentTitle(mTinyDB.getString(Constants.SAVED_TITLE))
.setContentText(mTinyDB.getString(Constants.SAVED_MESSAGE))
.setSmallIcon(R.drawable.ic_action_check)
.setContentIntent(pIntent)
.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, builder);
}
} }
Am I doing this completely wrong? 我这样做是完全错误的吗? I can't get anything to come up (at least at the set time). 我什么都没想(至少在设定的时间)。 Help or guidance is appreciated :) 帮助或指导表示赞赏:)
You're using PendingIntent.getService
, but your intent is not for a service. 您正在使用PendingIntent.getService
,但您的意图并非用于服务。 For a BroadcastReceiver
, you should be using PendingIntent.getBroadcast
. 对于BroadcastReceiver
,您应该使用PendingIntent.getBroadcast
。
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
The problem you're running into is that you can't set an exact , repeatable alarm in Android. 您遇到的问题是,您无法在Android中设置确切的 ,可重复的警报。 If you want your repeating alarm to occur at an exact time, you must set a one time exact alarm and recreate it after the alarm goes off in your code. 如果希望重复警报在确切时间发生,则必须设置一次精确警报,并在代码中的警报消失后重新创建。
Citation: Documentation for setRepeating 引用: setRepeating的文档
In order for the system to be able to launch a component, it should be registered in your manifest: 为了使系统能够启动组件,应在清单中注册该组件:
AndroidManifest.xml AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypackage.myapplication" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".Notify" />
</application>
</manifest>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.