getInstallerPackageName 返回 null [英] getInstallerPackageName returns null

查看:14
本文介绍了getInstallerPackageName 返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Android 应用中,我想检查应用安装的来源.我查看了 Android 文档,发现以下 API 作为 PackageManager 类的一部分:

<块引用>

abstract String getInstallerPackageName(String packageName)

检索安装包的应用程序的包名.

然后我在MyActivity的onCreate方法中使用了如下代码:

if(Build.VERSION.SDK_INT >= 11){PackageManager myapp= this.getPackageManager();String installer = myapp.getInstallerPackageName("com.MyPackage");如果(安装程序 == 空){Toast.makeText(getApplicationContext(), MyActivity.this.getString(R.string.invalidsource), Toast.LENGTH_SHORT).show();MyActivity.this.finish();}}

但看起来这个 API 在真实设备上返回 null.

如果我们有任何其他方式获取应用安装源,请告诉我.

解决方案

如果你想测试这个方法是使用

adb push 'test.apk' '/sdcard/Download'

然后通过输入adb shell"并在终端中按回车键进入adb shell,然后使用以下命令进行安装

pm install -i 'installernameyouwant'/sdcard/Download/test.apk

在上述方法中安装应用程序时,调用 getInstallerPackageName() 返回您在上述命令中提供的安装程序名称

In my Android app I wanted to check the source of app installation. I looked at the Android documentation and found the following API as part of the PackageManager class:

abstract String getInstallerPackageName(String packageName) 

Retrieve the package name of the application that installed a package.

Then I used the following code in the onCreate method of MyActivity:

if(Build.VERSION.SDK_INT >= 11) 
{
    PackageManager myapp= this.getPackageManager(); 
    String installer = myapp.getInstallerPackageName("com.MyPackage");
    if(installer == null)
    {
        Toast.makeText(getApplicationContext(), MyActivity.this.getString(R.string.invalidsource), Toast.LENGTH_SHORT).show();
        MyActivity.this.finish();
    }
}

But it looks like this API returns null on a real device.

Please let me know if we have any other means of getting the source of app installation.

解决方案

if you want to test this the way is to store the apk on the device by using the

adb push 'test.apk' '/sdcard/Download'

then go to the adb shell by typing in 'adb shell' and hitting enter in the terminal then install using the following command

pm install -i 'installernameyouwant' /sdcard/Download/test.apk

when installing the app in above method the call to getInstallerPackageName() returns the installer name you gave in the above command

这篇关于getInstallerPackageName 返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆