我试图创建一个健身应用程序的一部分,将提醒用户在选定的时间进行他们的日常锻炼。这是利用AlarmManager创建每日警报,该警报将创建将在所选时间出现的通知。
当闹钟时间到来时,没有通知。我已经测试了通知,如果单击某个按钮,它就会出现。
我应该使用扩展BroadcastReceiver而不是扩展服务吗?
创建警报:
public void SaveAlarm(View V) {
Intent myIntent = new Intent(SetAlarm.this, NotifyService.class);
AlarmManager mAlarmManager = (AlarmManager) this.getSystemService(ALARM_SERVICE);
mPendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
//Create Alarm Time in calendar
Calendar mCalendar = Calendar.getInstance();
mCalendar.setTimeInMillis(System.currentTimeMillis());
mCalendar.set(Calendar.HOUR_OF_DAY, AlarmHour);
mCalendar.set(Calendar.MINUTE, AlarmMinute);
//Set alarm to repeat ever 24hrs
mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, mCalendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, mPendingIntent);
//Save Alarm to shared preferences
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("AlarmHour", AlarmHour);
editor.putInt("AlarmMinute", AlarmMinute);
editor.commit();
//Notify user it's been saved
Toast.makeText(this, "Alarm Saved", Toast.LENGTH_LONG).show();
//Switch view
Intent intent = new Intent(SetAlarm.this, Home.class);
startActivity(intent);
}
要创建通知的类:
public class NotifyService extends Service {
@Override
public IBinder onBind(Intent Intent) {
return null;
}
@Override
public void onCreate() {
createNotificationChannel();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "Workout")
.setSmallIcon(R.drawable.common_google_signin_btn_text_light)
.setContentTitle("Workout Time!")
.setContentText("Time to do your daily workout.")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
notificationManager.notify(1, builder.build());
}
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Workout";
String description = "Workout";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("Workout", name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
根据Service
组件的官方Android文档:
服务是一种应用程序组件,表示应用程序希望在不与用户交互的情况下执行较长时间运行的操作,或者为其他应用程序提供使用的功能。
在您的情况下,触发简单通知不被视为长时间运行,因此您应该使用BroadcastReceiver
,它旨在在特定条件下运行简单任务。
要使用BroadcastReceiver
实现此功能,您应首先将服务更改为BroadcastReceiver
:
public class NotifyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Notify user here
}
}
然后将接收器传递给AlarmManager
,如下所示:
public void SaveAlarm(View V) {
Intent myIntent = new Intent(SetAlarm.this, NotifyReceiver.class);
AlarmManager mAlarmManager = (AlarmManager) this.getSystemService(ALARM_SERVICE);
mPendingIntent = PendingIntent.getBroadcast(SetAlarm.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
...
}
别忘了在AndroidManifest.xml
声明你的接收器:
<receiver android:name=".NotifyReceiver"/>