求解,关于用Intent隐式调用ACTION_VIEW使程序崩溃

zhhnever 2016-07-12 03:02:47
写的一个小程序,检测到新版本并自动升级安装新版本APK的时候,就会报错,然后程序只接崩溃.
错误代码:
07-12 14:57:28.915 29493-29493/com.newapp.dodocom.consolyc E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.newapp.dodocom.consolyc, PID: 29493
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///storage/sdcard0/mobilesafe.apk typ=application/vnd.android.package.archive flg=0x10000000 }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1765)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1485)
at android.app.Activity.startActivityForResult(Activity.java:3742)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
at android.app.Activity.startActivityForResult(Activity.java:3703)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:871)
at android.app.Activity.startActivity(Activity.java:4013)
at android.app.Activity.startActivity(Activity.java:3981)
at com.newapp.dodocom.consolyc.SplashActivity.installApk(SplashActivity.java:248)
at com.newapp.dodocom.consolyc.SplashActivity.access$500(SplashActivity.java:35)
at com.newapp.dodocom.consolyc.SplashActivity$4$1.onSuccess(SplashActivity.java:132)
at com.newapp.dodocom.consolyc.SplashActivity$4$1.onSuccess(SplashActivity.java:120)
at net.tsz.afinal.http.HttpHandler.onProgressUpdate(HttpHandler.java:148)
at net.tsz.afinal.core.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:503)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5238)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:906)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:701)

安装APK的代码:
private void installApk(File t) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(t),"application/vnd.android.package.archive");
startActivity(intent);
}
...全文
370 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhhnever 2016-07-12
  • 打赏
  • 举报
回复
引用 5 楼 qq_31952445 的回复:
不用Flags呢
我开始是没有加Flags的.
zhhnever 2016-07-12
  • 打赏
  • 举报
回复
好像我在stackoverflow找到正解了 you are running this code on an Android environment that lacks the Google Play Store, such as an emulator, Kindle Fire, etc. If you are encountering this on an emulator, test this code path on a device that has the Play Store. If you are encountering this on some piece of hardware that lacks the Play Store, or if you are planning on distributing your app to devices that lack the Play Store, either handle the exception or use PackageManager and resolveActivity() to determine if your Intent will succeed before calling startActivity().
if(intent.resolveActivity(getPackageManager()) != null)
    startActivityForResult(intent, 0);
else
raining-like 2016-07-12
  • 打赏
  • 举报
回复
不用Flags呢
passself 2016-07-12
  • 打赏
  • 举报
回复
除了action 需要加Data 和Type
zhhnever 2016-07-12
  • 打赏
  • 举报
回复
   /**
     * 安装应用
     * @param安装的文件
     */;
    private void installApk(File t) {
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(android.content.Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(t),"application/vnd.android.package.archive");
        startActivity(intent);
    }
zhhnever 2016-07-12
  • 打赏
  • 举报
回复
可是我试了好几台真机,都不行...
zhhnever 2016-07-12
  • 打赏
  • 举报
回复
在百度了很多,说的是隐式调用ACTION_VIEW的崩溃,ROM里面并没有这个Activity才会这样.

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧