Android development: disable “show notification” programmatically with root permission?

纵然是瞬间 提交于 2019-11-30 05:31:43

Firstly it seems that it depends on which version of Android you are interested in. It seems that things changed in 4.3. I am investigating the latest master branch (which is l-preview), so going down the AOSP rabbit hole we find...

In the Settings package...

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
    }
}

In 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));
    }
}

Further in frameworks/base...

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

124: public static final int OP_POST_NOTIFICATION = 11;

Deeper in frameworks/base...

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

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

So if you have root you can modify the /data/system/appops.xml. You may need to study the format by reading the code at: https://github.com/android/platform_frameworks_base/blob/master/core/java/android/app/AppOpsManager.java

I found a pretty long article here with some information that might help as well:

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!