Android开发:使用root权限以编程方式禁用“show notification”?

时间:2014-07-04 15:46:32

标签: android notifications settings root

有没有办法以编程方式更改应用的详细信息(设置 - >应用 - > [anApp])? 具体来说,我可以取消选中"显示通知"?

enter image description here

我假设您拥有root权限

提前感谢您的帮助

1 个答案:

答案 0 :(得分:9)

首先,它似乎取决于您感兴趣的Android版本。似乎4.3中的内容发生了变化。我正在调查最新的master分支(这是l-preview),所以我们找到AOSP兔子洞......

在“设置”包中...

InstalledAppDetails.java:1299

private void setNotificationsEnabled(boolean enabled) {
    String packageName = mAppEntry.info.packageName;
    INotificationManager nm = INotificationManager.Stub.asInterface(
            ServiceManager.getService(Context.NOTIFICATION_SERVICE));
    try {
        final boolean enable = mNotificationSwitch.isChecked();
        nm.setNotificationsEnabledForPackage(packageName, mAppEntry.info.uid, enabled);
    } catch (android.os.RemoteException ex) {
        mNotificationSwitch.setChecked(!enabled); // revert
    }
}

在frameworks / base ...

NotificationManagerService.java:454

public void setNotificationsEnabledForPackage(String pkg, int uid, boolean enabled) {
    checkCallerIsSystem();

    Slog.v(TAG, (enabled?"en":"dis") + "abling notifications for " + pkg);

    mAppOps.setMode(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg,
            enabled ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_IGNORED);

    // Now, cancel any outstanding notifications that are part of a just-disabled app
    if (ENABLE_BLOCKED_NOTIFICATIONS && !enabled) {
        cancelAllNotificationsInt(pkg, 0, 0, true, UserHandle.getUserId(uid));
    }
}

进一步在框架/基础......

base/core/java/android/app/AppOpsManager.java

124: public static final int OP_POST_NOTIFICATION = 11;

更深入的框架/基础......

base/services/java/com/android/server/am/ActivityManagerService.java

2000: mAppOpsService = new AppOpsService(new File(systemDir, "appops.xml"));

因此,如果您有root,则可以修改/data/system/appops.xml。您可能需要阅读以下代码来研究格式:https://github.com/android/platform_frameworks_base/blob/master/core/java/android/app/AppOpsManager.java

我在这里发现了一篇很长的文章,其中包含一些可能有用的信息:

http://commonsware.com/blog/2013/07/26/app-ops-developer-faq.html