zoukankan      html  css  js  c++  java
  • android 开发 实现自动安装

    场景:实现自动安装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();
            }
        }
  • 相关阅读:
    索引
    互联网技术中的算法摘要
    Struts2(六)
    Struts2(五)
    Struts2(四)
    Struts2(三)
    Struts2(二)
    Struts2(一)
    WebService(三)
    WebService(二)
  • 原文地址:https://www.cnblogs.com/feijian/p/4476160.html
Copyright © 2011-2022 走看看