Android 16前台服务中全屏Intent无法正常触发问题求助
Android 16前台服务中全屏Intent无法正常触发问题求助
我遇到了一个头疼的问题:我的前台服务在Android 15及以下版本都能正常工作,但升级到Android 16后,全屏Intent突然失效了。我翻遍了相关文档,都没找到Android 16针对这部分的变更说明,有没有大佬能帮忙看看?
下面是我触发全屏通知的核心代码:
Intent fullScreenIntent = new Intent(context, StopAlert.class); fullScreenIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(context, 0, fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.ic_notification) .setContentTitle("Battery Charged") .setPriority(NotificationCompat.PRIORITY_MAX) .setCategory(NotificationCompat.CATEGORY_ALARM) .setFullScreenIntent(fullScreenPendingIntent, true); // NOT WORKING ON ANDROID 16 NotificationManagerCompat.from(context).notify(1, builder.build());
我已经在Manifest里声明了这些权限:
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
服务在Manifest中的配置如下:
<service android:name=".BatteryMonitorService" android:enabled="true" android:exported="false" android:foregroundServiceType="specialUse" android:value="Required to run the Battery Monitor Service and alert user of overcharging."> <intent-filter> <action android:name="com.onesilicondiode.batterywise.STOP_ACTION" /> </intent-filter> </service>
最后是完整的onStartCommand方法(部分内容未完整粘贴):
public int onStartCommand(Intent intent, int flags, int startId) { // Fix 1: Service killed by system - re-register receiver if null if (batteryReceiver == null) { batteryReceiver = new BroadcastReceiver() { @SuppressLint("MissingPermission") @Override public void onReceive(Context context, Intent intent) { BatteryManager batteryManager = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE); int batteryPercent = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY); int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); boolean isPlugged = plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS; int selectedBatteryLevel = prefs.getInt("selectedBatteryLevel", 85); boolean userStarted = prefs.getBoolean(USER_STARTED_KEY, DEFAULT_USER_STARTED); boolean alertPlayed = prefs.getBoolean(ALERT_PLAYED_KEY, false); Log.d(TAG, "onReceive: batteryPercent=" + batteryPercent + ", isPlugged=" + isPlugged + ", selectedBatteryLevel=" + selectedBatteryLevel + ", userStarted=" + userStarted + ", alertPlayed=" + alertPlayed); if (isPlugged && batteryPercent >= selectedBatteryLevel && !alertPlayed && !userStarted) { // Check notification permission first if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && ContextCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) { Log.w(TAG, "Missing notification permission"); return; } Intent fullScreenIntent = new Intent(context, StopAlert.class); fullScreenIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); PendingIntent fullScreenPendingIntent = PendingIntent.getActivity( context, STOP_ACTION_NOTIFICATION_ID, fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); NotificationCompat.Builder builder = new NotificationCompat.Builder(context, STOP_ACTION_CHANNEL_ID) .setSmallIcon(R.drawable.ic_notification) .setContentTitle("Battery Charged") .setContentText("Disconnect the charger") .setPriority(NotificationCompat.PRIORITY_MAX) .setCategory(NotificationCompat.CATEGORY_ALARM) .setAutoCancel(true) .setFullScreenIntent(fullScreenPendingIntent, true) .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC); Log.d(TAG, "onReceive: Posting full-screen notification"); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); try { notificationManager.notify(STOP_ACTION_NOTIFICATION_ID, builder.build()); Log.d(TAG, "Notification posted successfully"); } catch (SecurityException e) { Log.e(TAG, "Failed to post notification", e); } SharedPreferences.Editor editor = prefs.edit(); editor.putBoolean(ALERT_PLAYED_KEY, true); editor.putBoolean(USER_STARTED_KEY, false); editor.apply(); } else if (!isPlugged) { // 原代码内容未完整粘贴 } } }; } // 服务其他逻辑省略 return START_STICKY; }
内容来源于stack exchange
相关产品推荐
相关产品推荐

