android 下载安装并打开apk

package com.orangewealth.orangeclient.manager;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;



import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**  * Created by yangyang on 4/11/16.  */ public class DownPackageManager {
    private static final String TAG=DownPackageManager.class.getSimpleName();
    private static DownPackageManager instance;

    private DownPackageManager(){

    }

    private static DownPackageManager getInstance(){
        if(instance==null){
            instance=new DownPackageManager();
        }
        return instance;
    }

    //下载文件  public static File  getFileFromSercer(String path,ProgressDialog pd) throws Exception{
        if(Environment.getExternalStorageState().equals((Environment.MEDIA_MOUNTED))){
            String apkFile = Uri.parse(path).getLastPathSegment()+".apk";
            URL url=new URL(path);
            HttpURLConnection conn=(HttpURLConnection)url.openConnection();
            conn.setConnectTimeout(5000);
            pd.setMax(conn.getContentLength());
            InputStream is=conn.getInputStream();
            File file=new File(Environment.getExternalStorageDirectory(),apkFile);
            FileOutputStream fos=new FileOutputStream(file);
            BufferedInputStream bis=new BufferedInputStream(is);
            byte[] buffer =new byte[1024];
            int len;
            int total=0;
            while ((len=bis.read(buffer))!=-1){
                fos.write(buffer,0,len);
                total+=len;
                //  pd.setProgress(total);
            }
            fos.close();
            bis.close();
            is.close();
            return file;

        }else {
            return null;
        }
    }
    //安装指定文件的包  public static void installApk(Context context, File file){
        Intent intent=new Intent();

        intent.setAction(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        context.startActivity(intent);

    }
    //根据包名判断安装包是否安装  public static boolean isAppInstalled(Context c,String packgename){
        PackageInfo packageinfo;
        try{
            packageinfo=c.getPackageManager().getPackageInfo(packgename,0);
        }catch (Exception e){
            packageinfo=null;
            e.getStackTrace();
        }
        if (packageinfo == null) {
            //System.out.println("没有安装");  return false;
        } else {
            //System.out.println("已经安装");  return true;
        }
    }
    //下载APK  public static void downLodeApk(final Context context,final String url){
        final ProgressDialog pd;
        pd=new ProgressDialog(context);
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setMessage("正在下载应用");
        pd.show();
        pd.setCanceledOnTouchOutside(false);
        // 因为新启的线程,所以要是final  new Thread(){
            @Override
            public void run(){
                try{
                    File file=getFileFromSercer(url, pd);
                    installApk(context,file);
                    pd.dismiss();
                }catch (Exception e){
                    Toast.makeText(context.getApplicationContext(), "下载新版本失败", Toast.LENGTH_SHORT).show();

                }
            }

        }.start();

    }
    // 根据下载地址打开APK  public static void openApk( Context c,String packagenaem,Bundle bundle,String activity,String url){
        if(isAppInstalled(c,packagenaem)){
            Intent i=new Intent();
            ComponentName cn=new ComponentName(packagenaem,activity);
            i.setComponent(cn);
            i.putExtras(bundle);
            c.startActivity(i);
        }else {//未安装  downLodeApk(c,url);
        }
    }



}

你可能感兴趣的:(android)