android 下载安装并打开apk

该博客详细介绍了如何在Android设备上实现APK文件的下载、安装以及通过包名检查应用是否已安装。提供了相关的方法,包括从服务器下载APK,使用ProgressDialog显示下载进度,以及安装APK的Intent操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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



}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值