getInstallerPackageName返回null [英] getInstallerPackageName returns null

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

问题描述

在我的Andr​​oid应用程序,我想检查应用程序的安装源。 我看了看Android的文档,并检查下列API的一部分 PackageManager类

 抽象字符串getInstallerPackageName(字符串的packageName)
           检索安装了这个应用程序的包名。
 

然后我用下面的code在MyActivity的onCreate方法

 如果(Build.VERSION.SDK_INT> = 11)
    {
        PackageManager的myapp = this.getPackageManager();
        串安装= myapp.getInstallerPackageName(com.MyPackage);
        如果(安装== NULL)
        {
            Toast.makeText(getApplicationContext(),MyActivity.this.getString(R.string.invalidsource),Toast.LENGTH_SHORT).show();
            MyActivity.this.finish();
        }
    }
 

但它看起来像这个API返回的实际设备空。

请让我知道,如果我们有一个获得应用程序的安装源的任何其他手段。

解决方案

对于你的问题是如何将应用程序安装到设备上?通过亚行安装这将是空。

使用下面的你应该能够将其覆盖,以任何你想要进行测试。

 时安装-i installername com.example.package
 

GooglePlay应具有com.google.android.feedback的值。我不知道其他的应用程序商店使用的值。

In my Android app I wanted to check the source of app installation. I looked at the Android documentation and checked the following API as part of 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 in real device.

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

解决方案

With regard to your question how was the app installed onto the device? Through "adb install" it will be null.

Using the following you should be able to override it to whatever you want for testing purposes.

pm install -i installername com.example.package

GooglePlay should have a value of com.google.android.feedback. I'm not sure what values other app stores use.

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

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