Android - 以编程方式使用结果安装应用程序

时间:2014-06-26 13:59:29

标签: java android

我是Android新手,我有一个应用程序,它包含一个服务,在某些方面,服务需要安装一个新的.apk(基本上是一个自动更新),目前安装完成就像在下面的代码中一样不允许知道它何时完成或从中得到结果我需要知道为了执行其他驱动该结果的行为。

File mFile = new File(Uri.parse(downloadedPackageUriString).getPath());
Intent promptInstall = new Intent(Intent.ACTION_VIEW)
        .setDataAndType(Uri.fromFile(mFile), "application/vnd.android.package-archive")
        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
appContext.startActivity(promptInstall);

我想知道是否有办法做到这一点? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

逻辑可能是:

  1. 安装脚本
  2. 等待5秒
  3. 运行appInstalledOrNot功能
  4. 如果未安装,请在1秒后重试
  5. 这可能是代码:

    public void installApp(){
        File mFile = new File(Uri.parse(downloadedPackageUriString).getPath());
        Intent promptInstall = new Intent(Intent.ACTION_VIEW)
                .setDataAndType(Uri.fromFile(mFile), "application/vnd.android.package-archive")
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        appContext.startActivity(promptInstall);
    
        Thread t = new Thread(){
            public void run(){
    
                boolean installed = checkInstalled("vnd.android.package-archive");
                while(!installed)
                {
                    System.out.println("Not Installed Yet..Waiting 1 Second");
                    t.sleep(1000);
                }
    
                //Now it has been installed
                if(installed) {
                    //This intent will help you to launch if the package is already installed
                    Intent LaunchIntent = getPackageManager()
                        .getLaunchIntentForPackage("com.Ch.Example.pack");
                    startActivity(LaunchIntent);
    
                    System.out.println("App already installed on your phone");        
                }
                else {
                    System.out.println("App is not installed on your phone");
                }
                }
        t.start();
    }
    
    private boolean checkInstalled(String uri) {
        PackageManager pm = getPackageManager();
        boolean app_installed = false;
        try {
            pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
            app_installed = true;
        }
        catch (PackageManager.NameNotFoundException e) {
            app_installed = false;
        }
        return app_installed ;
    }
    

    代码基于:How to check programmatically if an application is installed or not in Android?

相关问题