触发通知的唤醒设备

问题描述 投票:0回答:1

我已经在我的应用中设置了提醒,但是当出现通知时,屏幕不会打开,也不会弹出。我只是收到通知通知栏。设备应唤醒(如果已锁定)或显示通知弹出窗口(如果已解锁)

AlarmReceiver类:

public class AlarmReceiver extends BroadcastReceiver {
    private final String CHANNEL_ID="Reminder";
    @Override
    public void onReceive(Context context, Intent intent) {
        //wake
        WakeLocker.acquire(context);

        int notificationId = intent.getIntExtra("notificationId", 0);
        Intent mainIntent = new Intent(context, Reminder.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, mainIntent, 0);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "Reminder";
            String description = "Reminder for Workout";

            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, name, importance);
            notificationChannel.setDescription(description);
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(notificationChannel);

            //FOR ANDROID OLDER THAN VERSION OREO (8.0)
            NotificationManager mynotificationManager =
                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);


            NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID);
            builder.setSmallIcon(android.R.drawable.ic_lock_idle_alarm)
                    .setContentTitle("It's Time")
                    .setContentText("Let's Workout")
                    .setWhen(System.currentTimeMillis())
                    .setAutoCancel(true)
                    .setContentIntent(contentIntent)
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                    .setDefaults(NotificationCompat.DEFAULT_ALL)
                    .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
            mynotificationManager.notify(notificationId, builder.build());
            //wake
            WakeLocker.release();
        }

    }
}

WakeLocker类:​​

public abstract class WakeLocker {
    private static PowerManager.WakeLock wakeLock;

    public static void acquire(Context context) {


        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK |
                PowerManager.ACQUIRE_CAUSES_WAKEUP |
                PowerManager.ON_AFTER_RELEASE, "HomeFitness:WAKE_LOCK_TAG");
        wakeLock.acquire(1000);
    }

    public static void release() {
        if (wakeLock != null) wakeLock.release(); wakeLock = null;
    }
}

提醒类:

public void onClick(View v)
    {
        TimePicker t=findViewById(R.id.timepicker);
        Intent intent=new Intent(Reminder.this,AlarmReceiver.class);
        intent.putExtra("notificationId",notificationId);
        PendingIntent alarmIntent=PendingIntent.getBroadcast(Reminder.this, 0 , intent , PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager alarm=(AlarmManager) getSystemService(ALARM_SERVICE);


        switch(v.getId())
        {
            case R.id.set:
                int hr= t.getHour();
                int min=t.getMinute();

                Calendar startTime = Calendar.getInstance();
                startTime.set(Calendar.HOUR_OF_DAY,hr);
                startTime.set(Calendar.MINUTE,min);
                startTime.set(Calendar.SECOND,0);
                startTime.set(Calendar.MILLISECOND,0);


                long alarmStartTime=startTime.getTimeInMillis();
                alarm.set(AlarmManager.RTC_WAKEUP,alarmStartTime,alarmIntent);

                alarm.setRepeating(AlarmManager.RTC_WAKEUP, startTime.getTimeInMillis(),
                        AlarmManager.INTERVAL_DAY, alarmIntent);
                //added later for higher android
                alarm.setExact(AlarmManager.RTC_WAKEUP,startTime.getTimeInMillis(),alarmIntent);
                alarm.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,startTime.getTimeInMillis(),alarmIntent);
                //new close
                Toast.makeText(Reminder.this,"Reminder Set", Toast.LENGTH_SHORT).show();
                break;

            case R.id.cancelb:
                alarm.cancel(alarmIntent);
                Toast.makeText(Reminder.this, "Reminder Canceled", Toast.LENGTH_SHORT).show();
                break;
        }
    }
android broadcastreceiver alarmmanager powermanager
1个回答
0
投票

ACQUIRE_CAUSES_WAKEUP不能与PARTIAL_WAKE_LOCK一起使用。尝试删除后者。尝试使用.setPriority(NotificationCompat.PRIORITY_MAX)进行通知。

/**
 * Wake lock flag: Turn the screen on when the wake lock is acquired.
 * <p>
 * Normally wake locks don't actually wake the device, they just cause
 * the screen to remain on once it's already on.  Think of the video player
 * application as the normal behavior.  Notifications that pop up and want
 * the device to be on are the exception; use this flag to be like them.
 * </p><p>
 * Cannot be used with {@link #PARTIAL_WAKE_LOCK}.
 * </p>
 */
public static final int ACQUIRE_CAUSES_WAKEUP = 0x10000000;
© www.soinside.com 2019 - 2024. All rights reserved.