1

我想从我的应用程序启动安装在我的 android 设备上的其他应用程序的 BroadcastReceiver。这是代码

ComponentName componentName  = new ComponentName("com.example.myapp","com.example.myapp.MyBroadcastReceiver");
pkgManager.setComponentEnabledSetting(componentName,
                         PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                         PackageManager.DONT_KILL_APP);

“com.example.myapp”是包含“MyBroadcastReceiver”的其他应用程序的包名

我参考了帖子中给出的答案Android PackageManager enable any application and Android - can't enable BroadcastReceiver但它们似乎都没有工作。应用程序继续抛出权限拒绝 SecurityException。给定的是logcat

05-24 11:37:38.382: E/AndroidRuntime(8746): java.lang.SecurityException: Permission Denial: attempt to change component state from pid=8746, uid=10042, package uid=10041
4

1 回答 1

3
public void setComponentEnabledSetting(ComponentName componentName,  
            int newState, int flags) {  
        setEnabledSetting(componentName.getPackageName(),  
                componentName.getClassName(), newState, flags);  
    }  

public void setApplicationEnabledSetting(String appPackageName,  
            int newState, int flags) {  
        setEnabledSetting(appPackageName, null, newState, flags);  
    }  



private void setEnabledSetting(  
            final String packageName, String className, int newState, final int flags) {  
        if (!(newState == COMPONENT_ENABLED_STATE_DEFAULT  
              || newState == COMPONENT_ENABLED_STATE_ENABLED  
              || newState == COMPONENT_ENABLED_STATE_DISABLED)) {  
            throw new IllegalArgumentException("Invalid new component state: "  
                    + newState);  
        }  
        PackageSetting pkgSetting;  
        final int uid = Binder.getCallingUid();  
        final int permission = mContext.checkCallingPermission(  
                android.Manifest.permission.CHANGE_COMPONENT_ENABLED_STATE);  
        final boolean allowedByPermission = (permission == PackageManager.PERMISSION_GRANTED);  
        boolean sendNow = false;  
        boolean isApp = (className == null);  
        String componentName = isApp ? packageName : className;  
        int packageUid = -1;  
        ArrayList<String> components;  
        synchronized (mPackages) {  
            pkgSetting = mSettings.mPackages.get(packageName);  
            if (pkgSetting == null) {  
                if (className == null) {  
                    throw new IllegalArgumentException(  
                            "Unknown package: " + packageName);  
                }  
                throw new IllegalArgumentException(  
                        "Unknown component: " + packageName  
                        + "/" + className);  
            }  
            if (!allowedByPermission && (**uid != pkgSetting.userId**)) {  
                throw new SecurityException(  
                        "Permission Denial: attempt to change component state from pid="  
                        + Binder.getCallingPid()  
                        + ", uid=" + uid + ", package uid=" + pkgSetting.userId);  
            }  
....  
}  

它会比较uid,所以不能禁用或启用其他应用程序。 来自:/frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java

于 2012-10-17T08:31:02.367 回答