我正在尝试在警报10响起时使用android 10中的NotificationCompat设置 fullScreentIntent ,但是即使发生警报也不会显示fullScreenIntent。
我在 list 文件中添加了 USE_FULL_SCREEN_INTENT 。我尝试获取部分唤醒锁,禁用了键盘锁,但是全屏意图仍然无法正常工作。
AndroidManifest.xml
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
AlarmBroadcastReceiver.java
public class AlarmBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "AlarmBroadcastReceiver";
public static final String STOP_CHANNEL_ID = "Location Alert";
public static final int ALARM_NOTIFICATION_ID = 2;
public static MediaPlayer mp;
public static Vibrator vibrator;
private boolean isVibrationEnabled = false;
KeyguardManager keyguardManager;
@Override
public void onReceive(Context context, Intent intent) {
showStopNotification(context);
long[] mVibratePattern = new long[]{0, 400, 400, 400, 400, 400, 400, 400};
final int[] mAmplitudes = new int[]{0, 128, 0, 128, 0, 128, 0, 128};
isVibrationEnabled = intent.getExtras().getBoolean(LocationAlertService.IS_VIBRATE);
mp = MediaPlayer.create(context, R.raw.ring1);
mp.setLooping(true);
mp.start();
if(isVibrationEnabled) {
vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(VibrationEffect.createWaveform(mVibratePattern, mAmplitudes, 0));
} else {
//deprecated in API 26
vibrator.vibrate(mVibratePattern, 3);
}
}
}
public void showStopNotification(Context context) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
NotificationChannel stopServiceChannel = new NotificationChannel(
STOP_CHANNEL_ID,
"Location Alert Channel",
NotificationManager.IMPORTANCE_DEFAULT
);
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(stopServiceChannel);
Intent wakeupIntent = new Intent(context, WakeUpActivity.class);
wakeupIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent wakeupPendingIntent = PendingIntent.getActivity(context, 0, wakeupIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent mainIntent = new Intent(context, MainActivity.class);
wakeupIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent mainPendingIntent = PendingIntent.getActivity(context, 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
keyguardManager = (KeyguardManager) context.getSystemService(KEYGUARD_SERVICE);
PowerManager powerManager = (PowerManager) context.getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE,
"MyApp::" + TAG);
wakeLock.acquire(10000);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, STOP_CHANNEL_ID)
.setSmallIcon(R.drawable.location_alerter)
.setContentTitle(context.getResources().getText(R.string.stop_location_alert))
.setContentText(context.getResources().getText(R.string.click_to_stop_activity))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setFullScreenIntent(wakeupPendingIntent, true);
Notification alarmNotification = notificationBuilder.build();
notificationManager.notify(0, alarmNotification);
wakeLock.release();
}
else {
Intent wakeIntent = new Intent(context, WakeUpActivity.class);
wakeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(wakeIntent);
}
}
}
在 onReceive()上,我正在调用 showStopNotification(),它检查设备是否在android 10上运行。如果它在android 10上运行,我试图获取唤醒锁并使用 NotificationCompat.builder的setFullScreenIntent()启动事件。 我什至将通知优先级设置为高。
我不知道为什么在警报响起后不显示fullScreenIntent。声音和振动正常,但没有UI可以停止警报。在此先感谢您对我的帮助。
最佳答案
尝试将您的通知 channel (STOP_CHANNEL_ID)优先级提高为NotificationManager.IMPORTANCE_HIGH
关于broadcastreceiver - 当屏幕在android 10中锁定时,setFullScreenIntent无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59458602/