1. 程式人生 > >android 跳轉到應用通知設定介面

android 跳轉到應用通知設定介面

4.4以下並沒有提過從app跳轉到應用通知設定頁面的Action,可考慮跳轉到應用詳情頁面,下面是直接跳轉到應用通知設定的程式碼:

 if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    Intent intent = new Intent();
                    intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
                    intent.putExtra("app_package", getActivity().getPackageName());
                    intent.putExtra("app_uid", getActivity().getApplicationInfo().uid);
                    startActivity(intent);
                } else if (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
                    Intent intent = new Intent();
                    intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    intent.addCategory(Intent.CATEGORY_DEFAULT);
                    intent.setData(Uri.parse("package:" + getActivity().getPackageName()));
                    startActivity(intent);
                }

跳轉到應用詳情頁面的程式碼如下:

  Intent localIntent = new Intent();
                localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                if (Build.VERSION.SDK_INT >= 9) {
                    localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                    localIntent.setData(Uri.fromParts("package", getActivity().getPackageName(), null));
                } else if (Build.VERSION.SDK_INT <= 8) {
                    localIntent.setAction(Intent.ACTION_VIEW);
                    localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
                    localIntent.putExtra("com.android.settings.ApplicationPkgName", getActivity().getPackageName());
                }
                startActivity(localIntent);

對於檢測app是否開啟通知,同樣是針對4.4以上的系統才有效:

NotificationManagerCompat manager = NotificationManagerCompat.from(App.getInstance().getContext());
boolean isOpened = manager.areNotificationsEnabled();

4.4以下呼叫該方法並不會出錯,只是全部返回true,預設開啟狀態



Android 21 以上的處理不能完全支援Android O 版本以上的手機跳轉到通知設定介面。
參見原始碼:Input: EXTRA_APP_PACKAGE, the package to display. Constant Value: "android.provider.extra.APP_PACKAGE",其中渠道ID為可選,可不設定。見
Input: Optionally, {@link #EXTRA_CHANNEL_ID}, to highlight that channel.
因此對於Android O版本可做以下處理:
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
context.startActivity(intent);
}