Android PackageManager 启用任何应用程序
我开发了一个应用程序,在其中我启用了可以手动安装的任何应用程序
,但我的问题是我只想启用我自己的包名称,而不是任何其他应用程序包名称。
这是我使用过的代码。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将此代码添加到您的两个应用程序 mainfest 中。
因为它需要一个相同的用户ID
这会对你有所帮助。
Add this code in your both applications mainfest.
because it need a userid to be same
This will help you.
查看平台,我看到以下内容:
请注意,因为这是“signatureOrSystem”(并且是在系统中声明的),所以它只能由使用系统签名签名的应用程序使用,换句话说,您需要是 Android供应商(HTC、三星、摩托罗拉等)。有关更多信息,请参阅:
http://developer.android.com/guide /topics/manifest/permission-element.html
更新:
Gurjinder Singh Pabla 有正确的答案,如果您尝试更改自己的组件或应用程序的启用状态,那么您需要确保他们有与 AndroidManifest.xml 中设置的用户 ID 相同。如果您想禁用具有不同用户 ID 的其他软件包,您只需成为供应商即可。可以看到Android源码中PackageManagerService.java中的检查是:
Looking at the platform I see the following:
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: