使用AlarmManager设置提醒

时间:2019-07-14 19:04:20

标签: android

我正在设置警报管理器,但问题是广播接收器正在立即接收。我在这里添加我的代码,以便您可以了解这里发生的情况。设置由广播接收器立即接收到的alarmmanager广播后,它没有等待由Calendar实例设置的正确时间。

AlarmManager alarmMgr = (AlarmManager) AlarmActivity.this.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        // cal.add(Calendar.SECOND, 5);
        alarmMgr.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

// Cal是Calendar的实例

AlarmReceiver.java

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;


public class AlarmReceiver extends BroadcastReceiver {
    Config config;
    static String APPOINTMENT = "appo";
    Context con;
    @Override
    public void onReceive(Context context, Intent intent) {
        con = context;
        config = new Config(context);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, APPOINTMENT)
                .setSmallIcon(R.drawable.icon_noti)
                .setContentTitle("Sunmate Appointment Reminder")
                .setContentText("Appointment with "+config.getAppoDoc()+" at "+config.getAppoTime()+" on "+config.getAppoDate())
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);

        createNotificationChannel();

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        // notificationId is a unique int for each notification that you must define
        notificationManager.notify((int)System.currentTimeMillis(), builder.build());

        config.setAppo("","","");
    }

    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 = "appo";
            String description = "sunmate appo";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel channel = new NotificationChannel("appo", 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 = con.getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }
}

清单

<receiver
            android:name=".AlarmReceiver"
            android:directBootAware="true"
            android:enabled="true"
            android:exported="true">
        </receiver>
enter code here

0 个答案:

没有答案