87 lines
3.2 KiB
Java
87 lines
3.2 KiB
Java
package com.arpa.hndahesudintocctmsdriver.util;
|
|
|
|
import android.app.NotificationManager;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.net.Uri;
|
|
import android.os.Build;
|
|
import android.provider.Settings;
|
|
|
|
import androidx.core.app.NotificationManagerCompat;
|
|
|
|
/**
|
|
* 通知栏权限工具
|
|
*
|
|
* @author xuexiang
|
|
* @since 2019-09-04 14:00
|
|
*/
|
|
public final class NotifyUtils {
|
|
|
|
private NotifyUtils() {
|
|
throw new UnsupportedOperationException("u can't instantiate me...");
|
|
}
|
|
|
|
/**
|
|
* 通知栏权限是否获取
|
|
*
|
|
* @param context
|
|
* @return
|
|
*/
|
|
public static boolean isNotifyPermissionOpen(Context context) {
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
return NotificationManagerCompat.from(context).getImportance() != NotificationManager.IMPORTANCE_NONE;
|
|
}
|
|
return NotificationManagerCompat.from(context).areNotificationsEnabled();
|
|
}
|
|
|
|
/**
|
|
* 打开通知栏权限设置页面
|
|
*
|
|
* @param context
|
|
*/
|
|
public static void openNotifyPermissionSetting(Context context) {
|
|
try {
|
|
Intent intent = new Intent();
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
//直接跳转到应用通知设置的代码:
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
|
|
intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
|
|
intent.putExtra(Settings.EXTRA_CHANNEL_ID, context.getApplicationInfo().uid);
|
|
context.startActivity(intent);
|
|
return;
|
|
}
|
|
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);
|
|
context.startActivity(intent);
|
|
return;
|
|
}
|
|
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);
|
|
return;
|
|
}
|
|
|
|
//4.4以下没有从app跳转到应用通知设置页面的Action,可考虑跳转到应用详情页面,
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
|
|
intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
|
|
intent.setData(Uri.fromParts("package", context.getPackageName(), null));
|
|
context.startActivity(intent);
|
|
return;
|
|
}
|
|
|
|
intent.setAction(Intent.ACTION_VIEW);
|
|
intent.setClassName("com.android.settings", "com.android.setting.InstalledAppDetails");
|
|
intent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
|
|
context.startActivity(intent);
|
|
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|