Moh*_*fez 66 android android-notifications android-6.0-marshmallow
有什么方法可以启动我的应用程序的Android通知设置屏幕的意图(如下图所示)?或者一个简单的方法我可以制作一个PreferenceScreen项目,只需点击一下即可到达此处?
shh*_*hhp 108
以下内容适用于Android 5.0(Lollipop)及以上版本:
Intent intent = new Intent();
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
//for Android 5-7
intent.putExtra("app_package", getPackageName());
intent.putExtra("app_uid", getApplicationInfo().uid);
// for Android 8 and above
intent.putExtra("android.provider.extra.APP_PACKAGE", getPackageName());
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
注意:这在Android 5-7中不受官方支持,但它可以正常工作.从Android 8开始,它正式受到支持.此代码不向后兼容5.0之前的Android版本.
小智 55
我合并了Sergei和Shhp的解决方案以支持所有案例:
Intent intent = new Intent();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
intent.putExtra("app_package", context.getPackageName());
intent.putExtra("app_uid", context.getApplicationInfo().uid);
} else {
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setData(Uri.parse("package:" + context.getPackageName()));
}
context.startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
And*_*der 10
我已为Android 8.0 Oreo API 26或更高版本添加了频道通知设置.Android 4.4,KitKat提供了一个解决方案.
频道通知设置的用法:
// PRIMARY_CHANNEL:
goToNotificationSettings(getString(R.string.PRIMARY_CHANNEL), mContext);
// SECONDARY_CHANNEL:
goToNotificationSettings(getString(R.string.SECONDARY_CHANNEL), mContext);
Run Code Online (Sandbox Code Playgroud)
应用程序通知设置的用法:
goToNotificationSettings(null, mContext);
Run Code Online (Sandbox Code Playgroud)
goToNotificationSettings的方法:
public void goToNotificationSettings(String channel, Context context) {
Intent intent = new Intent();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
if (channel != null) {
intent.setAction(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_CHANNEL_ID, channel);
} else {
intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
}
intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (channel != null) {
intent.setAction(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_CHANNEL_ID, channel);
} else {
intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
}
intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
} else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
} else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
intent.putExtra("app_package", context.getPackageName());
intent.putExtra("app_uid", context.getApplicationInfo().uid);
} else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setData(Uri.parse("package:" + context.getPackageName()));
}
context.startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)
对于懒人来说,这是@Helix 的 kotlin 版本答案:
fun openAppNotificationSettings(context: Context) {
val intent = Intent().apply {
when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> {
action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
}
Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP -> {
action = "android.settings.APP_NOTIFICATION_SETTINGS"
putExtra("app_package", context.packageName)
putExtra("app_uid", context.applicationInfo.uid)
}
else -> {
action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
addCategory(Intent.CATEGORY_DEFAULT)
data = Uri.parse("package:" + context.packageName)
}
}
}
context.startActivity(intent)
}
Run Code Online (Sandbox Code Playgroud)
用Kotlin编写的更新版本,包括此处描述的所有案例。
fun showNotificationSettings(context: Context, channelId: String? = null) {
val notificationSettingsIntent = when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O /*26*/ -> Intent().apply {
action = when (channelId) {
null -> Settings.ACTION_APP_NOTIFICATION_SETTINGS
else -> Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS
}
channelId?.let { putExtra(Settings.EXTRA_CHANNEL_ID, it) }
putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P /*28*/) {
flags += Intent.FLAG_ACTIVITY_NEW_TASK
}
}
Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP /*21*/ -> Intent().apply {
action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
putExtra("app_package", context.packageName)
putExtra("app_uid", context.applicationInfo.uid)
}
Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT /*19*/ -> Intent().apply {
action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
addCategory(Intent.CATEGORY_DEFAULT)
data = Uri.parse("package:${context.packageName}")
}
else -> null
}
notificationSettingsIntent?.let(context::startActivity)
}
Run Code Online (Sandbox Code Playgroud)
你可以这样使用它
showNotificationSettings(context)
Run Code Online (Sandbox Code Playgroud)
或者您可以添加频道 ID 以显示频道设置
showNotificationSettings(context, channelId = "some_channel_id")
Run Code Online (Sandbox Code Playgroud)
我使用此代码(kitkat和下一版本):
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);
}
Run Code Online (Sandbox Code Playgroud)