Android PackageManager 启用任何应用程序

发布于 2024-12-20 04:46:38 字数 947 浏览 2 评论 0原文

我开发了一个应用程序,在其中我启用了可以手动安装的任何应用程序

,但我的问题是我只想启用我自己的包名称,而不是任何其他应用程序包名称。

这是我使用过的代码。

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

这里“com.service”是我安装的包名称,“com.service.EnableActivity”是我的第一个应用程序启动器Activity。

日志

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)

但是当我使用相同的应用程序包名和类名时,它工作正常。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜宝宝 2024-12-27 04:46:38

将此代码添加到您的两个应用程序 mainfest 中。

<manifest
  .......
  .......
  .......
  android:sharedUserLable="...."
  android:sharedUserID="...">

因为它需要一个相同的用户ID

这会对你有所帮助。

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.

我家小可爱 2024-12-27 04:46:38

查看平台,我看到以下内容:

<!-- 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" />

请注意,因为这是“signatureOrSystem”(并且是在系统中声明的),所以它只能由使用系统签名签名的应用程序使用,换句话说,您需要是 Android供应商(HTC、三星、摩托罗拉等)。有关更多信息,请参阅:

http://developer.android.com/guide /topics/manifest/permission-element.html

更新:

Gurjinder Singh Pabla 有正确的答案,如果您尝试更改自己的组件或应用程序的启用状态,那么您需要确保他们有与 AndroidManifest.xml 中设置的用户 ID 相同。如果您想禁用具有不同用户 ID 的其他软件包,您只需成为供应商即可。可以看到Android源码中PackageManagerService.java中的检查是:

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文