我正在寻找一种方法,可以直接从我的应用程序中打开 MIUI 应用程序通知设置 屏幕。
就像这样:设置 -> 已安装的应用程序 -> 我的应用程序 -> 通知。
如何构建意图以打开此屏幕?
这不同于在 Oreo 中添加的通知渠道管理器 - MIUI(小米)有自己的通知管理器。
我正在寻找一种方法,可以直接从我的应用程序中打开 MIUI 应用程序通知设置 屏幕。
就像这样:设置 -> 已安装的应用程序 -> 我的应用程序 -> 通知。
如何构建意图以打开此屏幕?
这不同于在 Oreo 中添加的通知渠道管理器 - MIUI(小米)有自己的通知管理器。
这段代码在MIUI 10中有效:
Intent settingsIntent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra(Settings.EXTRA_APP_PACKAGE, "YourPackage");
startActivity(settingsIntent);
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 O
intent.putExtra("android.provider.extra.APP_PACKAGE", getPackageName());
startActivity(intent);
构建打开通知设置的意图在不同的SDK版本上是不同的,您可以尝试下面的代码,在我的设备上运行良好,假设您在活动的上下文中运行此代码:
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//above android 8.0 jump to notification channels
if (Build.VERSION.SDK_INT >= 26) {
intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
intent.setData(Uri.fromParts("package", "your.package.name", null));
}
//android 5.0-7.0 notification settings
if (Build.VERSION.SDK_INT >= 21 && Build.VERSION.SDK_INT < 26) {
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
intent.putExtra("app_package", "your.package.name");
intent.putExtra("app_uid", getApplicationInfo().uid);
}
//others
if (Build.VERSION.SDK_INT < 21) {
intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
intent.setData(Uri.fromParts("package", "your.package.name", null));
}
startActivity(intent);
试试这个
在 Android Oreo 及以上版本(MIUI v9 和 v10)中打开应用通知设置,您需要在 intent
中传递NOTIFICATION_CHANNEL_ID
。
下面是在 MIUI v9 和新的 v10 版本中测试过的可行代码:
示例代码:
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.util.Objects;
public class MainActivity extends AppCompatActivity {
String NOTIFICATION_CHANNEL_ID = "Nilesh_channel";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
long pattern[] = {0, 1000, 500, 1000};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Your Notifications",
NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setDescription("");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(pattern);
notificationChannel.enableVibration(true);
Objects.requireNonNull(mNotificationManager).createNotificationChannel(notificationChannel);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = mNotificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID);
channel.canBypassDnd();
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setColor(ContextCompat.getColor(this, R.color.colorAccent))
.setContentTitle(getString(R.string.app_name))
.setContentText("Test")
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher_background)
.setAutoCancel(true);
mNotificationManager.notify(1000, notificationBuilder.build());
}
}
public void ClickMe(View view) {
notificationSettings(NOTIFICATION_CHANNEL_ID,this);
}
public void notificationSettings(String channel, Context context) {
Intent intent = new Intent();
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);
}
}
Intent
是什么;只发现了v7和v8的内容。回答一些其他问题可能有助于设置此问题的赏金。 - Martin Zeitler