Ram*_*thy 6 broadcastreceiver android-alarms android-fullscreen android-10.0
我正在尝试在闹钟响起时使用 android 10 中的 NotificationCompat设置fullScreentIntent,但即使闹钟响起时 fullScreenIntent 也没有显示。
我在清单文件中添加了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" />
Run Code Online (Sandbox Code Playgroud)
警报广播接收器.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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在onReceive() 上,我调用showStopNotification()来检查设备是否在 android 10 上运行。如果它在 android 10 上运行,我将尝试获取唤醒锁并使用NotificationCompat.builder 的 setFullScreenIntent()启动活动。我什至将通知优先级设置为高。
我不知道为什么闹钟响起后不显示fullScreenIntent。声音和振动正在工作,但没有用户界面来停止警报。预先感谢您帮助我。
| 归档时间: |
|
| 查看次数: |
1812 次 |
| 最近记录: |