无法提示在Android

时间:2017-05-14 08:25:22

标签: android file apk

我正在开发一个Android应用程序。在我的应用程序中,我正在检查MainActivity中最新apk的新版本。如果需要更新应用程序,我只需使用Retrofit从服务器下载apk文件,然后提示用户安装下载的apk文件。所有下载过程都没有任何问题。但是我遇到了提示用户的问题。我跟着this link

这是我在MainActivity中提示的方式

public void promptToInstallDownloadedApk(String path)
    {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File(path)), "application/com.football.waiyanhein.tonightfootballreport");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

当我运行代码时,它会抛出此错误。

  

.354 12750-12750 / com.football.waiyanhein.tonightfootballreport   E / AndroidRuntime:致命异常:主要                                                                                                  过程:com.football.waiyanhein.tonightfootballreport,PID:12750                                                                                                  android.content.ActivityNotFoundException:找不到要处理的Activity   意图{act = android.intent.action.VIEW   DAT =文件:///storage/emulated/0/Android/data/com.football.waiyanhein.tonightfootballreport/files/tonight_football_report.apk   典型值=应用/ com.football.waiyanhein.tonightfootballreport   flg = 0x10000000}                                                                                                      在   android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1798)                                                                                                      在   android.app.Instrumentation.execStartActivity(Instrumentation.java:1512)                                                                                                      在android.app.Activity.startActivityForResult(Activity.java:3917)                                                                                                      在   android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)                                                                                                      在   android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:77)                                                                                                      在android.app.Activity.startActivityForResult(Activity.java:3877)                                                                                                      在   android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)                                                                                                      在android.app.Activity.startActivity(Activity.java:4200)                                                                                                      在android.app.Activity.startActivity(Activity.java:4168)                                                                                                      在   com.football.waiyanhein.tonightfootballreport.MainActivity.promptToInstallDownloadedApk(MainActivity.java:912)                                                                                                      在   com.football.waiyanhein.tonightfootballreport.MainActivity.writeApkDownloadResponseBodyToDisk(MainActivity.java:893)                                                                                                      在   com.football.waiyanhein.tonightfootballreport.MainActivity $ 15.onResponse(MainActivity.java:850)                                                                                                      在   retrofit2.ExecutorCallAdapterFactory $ ExecutorCallbackCall $ 1 $ 1.run(ExecutorCallAdapterFactory.java:68)                                                                                                      在android.os.Handler.handleCallback(Handler.java:739)                                                                                                      在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                                      在android.os.Looper.loop(Looper.java:148)                                                                                                      在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                                                      at java.lang.reflect.Method.invoke(Native Method)                                                                                                      在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                                                      在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)05-14   04:19:18.359 679-1124 / system_process W / ActivityManager:强制   整理活动   com.football.waiyanhein.tonightfootballreport / .MainActivity 05-14   04:19:18.387 664-664 /? E / EGL_emulation:tid 664:   eglCreateSyncKHR(1215):错误0x3004(EGL_BAD_ATTRIBUTE)05-14   04:19:18.505 679-10967 / system_process I / OpenGLRenderer:已初始化   EGL,版本1.4

为什么我不能提示?我设置" application / com.football.waiyanhein.tonightfootballreport"促进。那是我的包裹名称。

我也试过了,但它没有用。

public void promptToInstallDownloadedApk(String path)
    {
        Intent promptInstall = new Intent(Intent.ACTION_VIEW)
                .setDataAndType(Uri.parse(path),
                        "application/vnd.android.package-archive");
        startActivity(promptInstall);
    }

1 个答案:

答案 0 :(得分:1)

最后一个例子应该有效。我正在使用它:

File updatedApk = new File(Environment.getExternalStorageDirectory(),
                        "your_file_name");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(updatedApk),
        "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);