场景:实现自动安装apk程序

注意:不能使用 intent.setDataAndType(Uri.parse(apkPath), "application/vnd.android.package-archive");

看代码:

/**     * 打开APK程序代码     * @param apkPath     */    public void openFile(String apkPath) {        Toast.makeText(mContext, "开始进行安装", Toast.LENGTH_SHORT).show();        try        {            Log.v("OpenFile", apkPath);            Intent intent = new Intent(Intent.ACTION_VIEW);              intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  //            intent.setDataAndType(Uri.parse(apkPath),  "application/vnd.android.package-archive");  //此处错误,apkPath是路径而不是Uri的字符串,因此报错android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW            File apkFile = new File(apkPath);            intent.setDataAndType(Uri.fromFile(apkFile),  "application/vnd.android.package-archive");              mContext.startActivity(intent);            android.os.Process.killProcess(android.os.Process.myPid());        }        catch(Exception ex)        {            ex.printStackTrace();        }    }

更多相关文章

  1. Android和Java判断字符串是否是中文
  2. android 9.0 获取U盘路径
  3. java|android加载src路径下面的图片文件
  4. Android开发_在Android Studio中搜索项目中出现过的字符串
  5. 字符串类的重量级实现——Rope的初步了解 - 学习Android - 51CTO
  6. Android 自定义文件路径选择器
  7. 修改Android Studio默认配置文件路径

随机推荐

  1. Android Camera Subsystem - startPrevie
  2. Android(安卓)studio使用NDK编译和调试(
  3. android studio获取SH1码
  4. Android 10 Camera -- 3A Modes and Stat
  5. 在线看Android系统源码,AndroidXRef的简易
  6. Android 中常用 MimeType 及对应文件类型
  7. android连接服务器下载文件工具类
  8. [Android Tools] Android Reverse Engine
  9. android中如何利用线程循环打印
  10. Android 8.1 通知的变化