问题
I have developed an application in which I have enabled that any application which we have can be installed manually
But my problem is that I only want to enable my own package name and rather not any other application package name.
Here is the code I have used.
try {
PackageManager pm1 = getPackageManager();
pm1.setComponentEnabledSetting(new ComponentName("com.service",
"com.service.EnableActivity"),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP
);
} catch (SecurityException e) {
e.printStackTrace();
}
here "com.service" is the package name that I install and "com.service.EnableActivity" is my first app launcherActivity.
Log
java.lang.SecurityException: Permission Denial: attempt to change component state from pid=3354, uid=10056, package uid=10058 at
android.os.Parcel.readException(Parcel.java:1322) at
android.os.Parcel.readException(Parcel.java:1276) at
android.content.pm.IPackageManager$Stub$Proxy.setComponentEnabledSettingIPackageManager.java:2217)
But when I use the same application packagename and classname then it's working fine.
回答1:
Add this code in your both applications mainfest.
<manifest
.......
.......
.......
android:sharedUserLable="...."
android:sharedUserID="...">
because it need a userid to be same
This will help you.
回答2:
Looking at the platform I see the following:
<!-- Allows an application to change whether an application component (other than its own) is
enabled or not. -->
<permission android:name="android.permission.CHANGE_COMPONENT_ENABLED_STATE"
android:label="@string/permlab_changeComponentState"
android:description="@string/permdesc_changeComponentState"
android:protectionLevel="signatureOrSystem" />
Note that because this is "signatureOrSystem" (and it is declared in the system) it can only be used by apps that are signed with the system signature, in other words you need to be an Android vendor (HTC, Samsung, Motorola, etc). See this for more info:
http://developer.android.com/guide/topics/manifest/permission-element.html
UPDATE:
Gurjinder Singh Pabla has the right answer, if you are trying to change the enabled state of your own components or applications then you need to ensure that they have the same user id, which is set in the AndroidManifest. You only need to be a Vendor if you want to disable other packages that have different user ids. You can see the check in the Android source in PackageManagerService.java is:
final int uid = Binder.getCallingUid();
final int permission = mContext.checkCallingPermission(
android.Manifest.permission.CHANGE_COMPONENT_ENABLED_STATE);
final boolean allowedByPermission = (permission == PackageManager.PERMISSION_GRANTED);
// ...
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);
}
来源:https://stackoverflow.com/questions/8445881/android-packagemanager-enable-any-application