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();
        }
    }

 

posted @ 2015-05-04 14:45  飞剑  阅读(939)  评论(0编辑  收藏  举报