我有一个要求,我需要以编程方式启用和禁用App Info的“显示通知”。我用Google搜索了很长时间,但无法得到正确的解决方案。这可以在android中以编程方式打开/关闭“显示通知”吗?提前谢谢。
答案 0 :(得分:1)
实际上,没有办法打开/关闭编程通知,这是我们使用以下代码执行此操作的唯一方法。
public class CustomToast {
private static final String CHECK_OP_NO_THROW = "checkOpNoThrow";
private static final String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";
public static void makeText(Context mContext, String message, int time) {
if (isNotificationEnabled(mContext)) {
//Show Toast message
Toast.makeText(mContext, message, time).show();
} else {
// Or show own custom alert dialog
showCustomAlertDialog(mContext, message);
}
}
private static boolean isNotificationEnabled(Context mContext) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
AppOpsManager mAppOps = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
ApplicationInfo appInfo = mContext.getApplicationInfo();
String pkg = mContext.getApplicationContext().getPackageName();
int uid = appInfo.uid;
Class appOpsClass;
try {
appOpsClass = Class.forName(AppOpsManager.class.getName());
Method checkOpNoThrowMethod =
appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE, String.class);
Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
int value = (int) opPostNotificationValue.get(Integer.class);
return ((int) checkOpNoThrowMethod.invoke(mAppOps, value, uid,
pkg) == AppOpsManager.MODE_ALLOWED);
} catch (ClassNotFoundException | NoSuchMethodException | NoSuchFieldException
| InvocationTargetException | IllegalAccessException ex) {
Utils.logExceptionCrashLytics(ex);
}
return false;
} else {
return false;
}
}
private static void showCustomAlertDialog(Context mContext, String message) {
if (!(mContext instanceof Activity && ((Activity)mContext).isFinishing())) {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(mContext);
mBuilder.setMessage(message);
mBuilder.setPositiveButton(mContext.getString(R.string.ok),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
mBuilder.setCancelable(true);
AlertDialog alertDialog = mBuilder.create();
alertDialog.show();
}
}
}
答案 1 :(得分:0)
在发布应用通知之前,您必须保留布尔标志并添加支票。在SharedPreferences中保存该布尔标志。用户或您的应用禁用/启用通知后,请将其反映在SharedPreferences中。
此外,您可以创建一个实用工具类来发布通知,这样您就不必在很多不同的地方添加支票。
public class NotificationUtil {
public static void showNotification(
Context context,
int notificationId,
int iconId,
Class parentStackClass,
String notificationTitle,
String notificationText
) {
boolean showNotification = PreferenceManager
.getDefaultSharedPreferences(context)
.getBoolean("SHOW_NOTIFICATION", true);
if (!showNotification) return;
android.support.v4.app.NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(iconId)
.setContentTitle(notificationTitle)
.setContentText(notificationText);
Intent resultIntent = new Intent(context, parentStackClass);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(parentStackClass);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(notificationId, mBuilder.build());
}
}
答案 2 :(得分:0)
我们无法以编程方式打开/关闭通知。我们可以使用以下代码段检查通知状态
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
ApplicationInfo appInfo = context.getApplicationInfo();
String pkg = context.getApplicationContext().getPackageName();
int uid = appInfo.uid;
Class appOpsClass;
try {
appOpsClass = Class.forName(AppOpsManager.class.getName());
Method checkOpNoThrowMethod =
appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE, String.class);
Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
int value = (int) opPostNotificationValue.get(Integer.class);
return ((int) checkOpNoThrowMethod.invoke(mAppOps, value, uid,
pkg) == AppOpsManager.MODE_ALLOWED);
} catch (ClassNotFoundException | NoSuchMethodException | NoSuchFieldException
| InvocationTargetException | IllegalAccessException ex) {
Utils.logExceptionCrashLytics(ex);
}
// checked
} else {
// unchecked
}
答案 3 :(得分:-3)
您可以将其保存在SharedPreferences中,每次要显示通知时都可以进行检查。