利用AlarmManager触发通知

时间:2019-02-27 15:51:47

标签: java android alarmmanager android-notifications

我正在尝试创建健身应用程序的一部分,该应用程序将提醒用户在选定的时间进行日常锻炼。这是利用AlarmManager来创建每日警报,该警报将创建一条在选定时间显示的通知。

警报时间到来时,没有通知通过。我已经测试了该通知,如果将其放在按钮的操作单击上,它将显示出来。

我应该使用扩展BroadcastReceiver 而不是扩展Service

创建警报:

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

}

1 个答案:

答案 0 :(得分:1)

根据Service组件的官方Android文档:

  

服务是一种应用程序组件,代表应用程序执行长时间运行的操作而不与用户交互或提供功能供其他应用程序使用的愿望。

在您的情况下,触发简单通知不被认为是一项长时间运行的操作,因此您应使用BroadcastReceiver,该操作旨在在特定条件下为简单任务运行。

要使用SetFactory("OpenCASCADE"); // Input Rectangle(1) = {0, 0, 0, 300, 300, 0}; Disk(2) = {50, 50, 0, 10, 10}; Disk(3) = {50,250,0,10,10}; Disk(4) = {250,250,0,10,10}; Disk(5) = {250,50,0,10,10}; Rectangle(6) = {30,30,146,240,240,10}; Rectangle(7) = {40,40,146,220,220,10}; // Start Operations s() = BooleanFragments{ Surface{1}; Delete; }{ Surface{2,3,4,5}; Delete;}; ext() = Extrude{0,0,300} {Surface{s()}; Layers{10}; Recombine;}; st() = BooleanFragments{ Surface{6}; Delete;}{Surface{7}; Delete;}; Recursive Delete {Surface{7}; } Extrude{0,0,10} {Surface{22}; Layers{10}; Recombine;} BooleanFragments{ Volume{5}; Delete;}{Volume{6}; Delete;} // Mesh Options all elements needs to be Hexa Mesh.RecombineAll = 2; 来实现此目的,您应该首先将服务更改为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
相关问题