在我的Android应用程序中,我想检查应用程序安装的来源.我查看了Android文档,发现以下API是PackageManager类的一部分:
Run Code Online (Sandbox Code Playgroud)abstract String getInstallerPackageName(String packageName)检索安装包的应用程序的包名称.
然后我在onCreateMyActivity 的方法中使用了以下代码:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
但看起来这个API null在真实设备上返回.
如果我们有任何其他方式获取应用程序安装源,请告诉我.
如果你想测试这个方法是通过使用的方式将apk存储在设备上
adb push'test.apk''/ sdcard/Download'
然后输入'adb shell'进入adb shell并点击进入终端然后使用以下命令安装
下午安装-i'installernameyouwant'/sdcard/Download/test.apk
在上面的方法中安装应用程序时,对getInstallerPackageName()的调用将返回您在上面的命令中给出的安装程序名称
@pmtmook用于"pm install"命令.
关于您的问题,应用程序是如何安装到设备上的?通过"adb install"它将为null.
使用以下内容,您应该能够将其覆盖到您想要的任何测试目的.
pm install -i installername com.example.package
Run Code Online (Sandbox Code Playgroud)
GooglePlay的值应为com.google.android.feedback.我不确定其他应用商店使用的值.