Android 中的提醒不重复
Reminder in Android doesn't repeat
我正在尝试在我的 Android 应用程序中设置一个提醒,它会在特定日期的同一设置时间响起。
第一次闹钟一切正常,但之后它不会重复,也不会在其他日子里响起。
界面如下:
点击"Save Changes",将为每个选定的日期调用 scheduleAlarm 方法:
private void scheduleAlarm(int dayOfWeek) {
Intent myIntent = new Intent(this , NotifyService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
if (dayOfWeek == 0)
alarmManager.cancel(pendingIntent);
else {
Calendar calendar = Calendar.getInstance();
String time_str[] = reminder_time.getText().toString().split(":");
calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time_str[0]));
calendar.set(Calendar.MINUTE, Integer.parseInt(time_str[1]));
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Long time = calendar.getTimeInMillis();
Long weekly = AlarmManager.INTERVAL_HOUR / 12; //AlarmManager.INTERVAL_DAY * 7;
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, time, weekly, pendingIntent);
}
}
如您在此版本中所见,我试图每 5 分钟重复一次闹钟 (Long weekly = AlarmManager.INTERVAL_HOUR / 12;
)
闹钟响起时调用的通知服务如下:
public class NotifyService extends Service {
public NotifyService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//TODO do something useful
return Service.START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
Intent intent = new Intent(this , Splash.class);
intent.putExtra(getString(R.string.NOTIFICATION), true);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.notification_icon_ensafe);
long[] pattern = {500,500,500,500,500,500,500,500,500};
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setContentTitle(getString(R.string.reminder))
.setContentText(getString(R.string.reminder_body))
.setLargeIcon(bm)
.setSmallIcon(R.drawable.notification_icon_ensafe)
.setContentIntent(contentIntent)
.setAutoCancel(true)
.setVibrate(pattern)
.setStyle(new NotificationCompat.InboxStyle())
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, mBuilder.build());
}
}
知道为什么它不起作用吗?
编辑
正如@Frank 所建议的,我实现了一个 BroadcastReceiver,但它从未被调用过。
在清单中:
<receiver android:name=".synchro.BootReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
class:
public class BootReceiver extends BroadcastReceiver {
List<Integer> selectedDays;
SharedPreferences preferences;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
**\ STUFF HAPPENS HERE \ **
}
}
}
broadcaster在上面列出的scheduleAlarm方法中发起,如下:
ComponentName receiver = new ComponentName(getApplicationContext(), BootReceiver.class);
PackageManager pm = getApplicationContext().getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
还有什么想法吗?
如果您的设备关闭,所有预定的闹钟都会被 OS 取消。这可能是你的问题吗?
要解决此问题,您必须使用广播侦听器来侦听设备启动,并在设备开始广播时再次安排闹钟。
* 编辑 *
您需要的所有信息都在这个页面上:https://developer.android.com/training/scheduling/alarms.html
特别是 "Start an Alarm When the Device Boots" 部分:"By default, all alarms are canceled when a device shuts down. To prevent this from happening..."
设置和测试需要一些时间。不急。
好的。我解决了问题。
在 NotifyService class 中,我使用 onCreate()
方法重新安排警报。此方法仅在第一次调用。之后只调用 onStartCommand
。
管理这两种方法我实现了一个有效的 AlarmAcheduler。
我正在尝试在我的 Android 应用程序中设置一个提醒,它会在特定日期的同一设置时间响起。 第一次闹钟一切正常,但之后它不会重复,也不会在其他日子里响起。
界面如下:
点击"Save Changes",将为每个选定的日期调用 scheduleAlarm 方法:
private void scheduleAlarm(int dayOfWeek) {
Intent myIntent = new Intent(this , NotifyService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
if (dayOfWeek == 0)
alarmManager.cancel(pendingIntent);
else {
Calendar calendar = Calendar.getInstance();
String time_str[] = reminder_time.getText().toString().split(":");
calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time_str[0]));
calendar.set(Calendar.MINUTE, Integer.parseInt(time_str[1]));
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Long time = calendar.getTimeInMillis();
Long weekly = AlarmManager.INTERVAL_HOUR / 12; //AlarmManager.INTERVAL_DAY * 7;
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, time, weekly, pendingIntent);
}
}
如您在此版本中所见,我试图每 5 分钟重复一次闹钟 (Long weekly = AlarmManager.INTERVAL_HOUR / 12;
)
闹钟响起时调用的通知服务如下:
public class NotifyService extends Service {
public NotifyService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//TODO do something useful
return Service.START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
Intent intent = new Intent(this , Splash.class);
intent.putExtra(getString(R.string.NOTIFICATION), true);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.notification_icon_ensafe);
long[] pattern = {500,500,500,500,500,500,500,500,500};
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setContentTitle(getString(R.string.reminder))
.setContentText(getString(R.string.reminder_body))
.setLargeIcon(bm)
.setSmallIcon(R.drawable.notification_icon_ensafe)
.setContentIntent(contentIntent)
.setAutoCancel(true)
.setVibrate(pattern)
.setStyle(new NotificationCompat.InboxStyle())
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, mBuilder.build());
}
}
知道为什么它不起作用吗?
编辑
正如@Frank 所建议的,我实现了一个 BroadcastReceiver,但它从未被调用过。
在清单中:
<receiver android:name=".synchro.BootReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
class:
public class BootReceiver extends BroadcastReceiver {
List<Integer> selectedDays;
SharedPreferences preferences;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
**\ STUFF HAPPENS HERE \ **
}
}
}
broadcaster在上面列出的scheduleAlarm方法中发起,如下:
ComponentName receiver = new ComponentName(getApplicationContext(), BootReceiver.class);
PackageManager pm = getApplicationContext().getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
还有什么想法吗?
如果您的设备关闭,所有预定的闹钟都会被 OS 取消。这可能是你的问题吗?
要解决此问题,您必须使用广播侦听器来侦听设备启动,并在设备开始广播时再次安排闹钟。
* 编辑 *
您需要的所有信息都在这个页面上:https://developer.android.com/training/scheduling/alarms.html
特别是 "Start an Alarm When the Device Boots" 部分:"By default, all alarms are canceled when a device shuts down. To prevent this from happening..."
设置和测试需要一些时间。不急。
好的。我解决了问题。
在 NotifyService class 中,我使用 onCreate()
方法重新安排警报。此方法仅在第一次调用。之后只调用 onStartCommand
。
管理这两种方法我实现了一个有效的 AlarmAcheduler。