2011-05-27 78 views
4

在我的应用程序中,我从我的活动调用一个JavaScript界面​​。在与此界面相关的webview中,我要求用户下载并安装APK。我正在使用“startActivityForResult”来启动安装过程(下载文件后)。这里是代码:安装APK以编程方式 - 返回值

Intent promptInstall = new Intent(Intent.ACTION_VIEW); 
     promptInstall.setDataAndType(Uri.fromFile(new File(PATH + "app.apk")), "application/vnd.android.package-archive"); 
     startActivityForResult(promptInstall, ACCEPT_INSTALL); 

我在主要活动类中使用“onActivityResult”来捕获上述活动的返回值。下面是代码:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    Log.d("requestCode, resultCode from Install APK: ", Integer.toString(requestCode) + ", " + Integer.toString(resultCode)); 
} 

我现在面临的问题是,“resultCode为”总是返回0,无论用户是否选择安装应用程序或不安装应用程序。我真的需要知道用户是否安装了应用程序。关于为什么返回值始终为0的任何想法?

我的最后一招是使用packagemanager检查在onActivityResult功能的应用程序的存在,以确定是否安装或没有应用程序,但我想知道为什么返回值始终为0

非常感谢...

+1

我也面临同样的问题,这是可笑的 – tasomaniac 2012-06-07 15:41:54

回答

3

我们开发了一个应用程序市场,并面临同样的问题。我们的解决方案是按照您的建议使用数据包管理器。

我查看了一些意图过滤器,因为在安装过程中似乎有一些意图。但我们无法让它运行。数据包管理器解决方案可以正常工作,因为您可以检查应用程序更新中重要的应用程序版本。

+0

谢谢你的回应。我将最终使用PackageManager来检查用户是否安装了应用程序。 – TheJag 2011-05-31 00:48:01

2

您可以检查firstInstallTime/lastUpdateTime和您的要求的时间比较PackageInstaller用于安装你的包,见下面的例子:

try { 

    PackageInfo installedPackageInfo = getPackageManager() 
      .getPackageInfo(installedPackageName, PackageManager.GET_ACTIVITIES); 
    long firstInstallTime = installedPackageInfo.firstInstallTime; 
    long lastUpdateTime = installedPackageInfo.lastUpdateTime; 

    // installationRequestTime is the time which you request for the PackageInstaller to install your package. This time should be got from System.currentTimeMillis() 
    return firstInstallTime > installationRequestTime || lastUpdateTime > installationRequestTime; 
} catch (PackageManager.NameNotFoundException e) { 
    // First time installation and user choose to not install the app 
    return false; 
} 

该解决方案是不漂亮,但不幸的是PackageInstaller唐我们没有提供明确的结果代码。