现在APP都有版本更新的功能,有的是跳转到浏览器进行下载,感觉比较麻烦,用户体验也不好,简便的方法就是直接下载安装即可。即使用Android自带的DownManager,通过启动一个后台服务进行下载,下载完成之后以广播的形式通知用户安装,方便很多。

比较闹心的还是6.0权限的问题,下载安装需要一个路径,也就需要读写SD卡权限,不过发现一个问题就是:如果自己不定义一个路径,6.0手机下载完成之后,并没有出现安装的界面,只有自己设置了路径才正常。。。

activity跳转到service进行下载

private Intent updataService ;private static int REQUESTPERMISSION = 110 ;public void upData(){   updataService = new Intent(SplashActivity.this,  UpdataService.class);   updataService.putExtra("downloadurl",version.url);   if(ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){//申请权限     ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUESTPERMISSION);        CommonToast.showToast("请允许权限进行下载安装");   }else{           startService(updataService);   }}@Overridepublic void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {    super.onRequestPermissionsResult(requestCode, permissions, grantResults);        if(requestCode==REQUESTPERMISSION){          if(permissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)){            if (grantResults[0] == PackageManager.PERMISSION_GRANTED){                if(updataService!=null)                     startService(updataService);             }else{//提示没有权限,安装不了咯            }         }    }}

updataService 类直接拿来用

public class UpdataService extends Service {    /** * 安卓系统下载类 **/    private DownloadManager manager;    /** * 接收下载完的广播 **/    private DownloadCompleteReceiver receiver;    private String url;    private String DOWNLOADPATH = "/apk/";//下载路径,如果不定义自己的路径,6.0的手机不自动安装    /** * 初始化下载器 **/    private void initDownManager() {        manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);        receiver = new DownloadCompleteReceiver();        //设置下载地址        DownloadManager.Request down = new DownloadManager.Request(Uri.parse(url));        // 设置允许使用的网络类型,这里是移动网络和wifi都可以        down.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE                | DownloadManager.Request.NETWORK_WIFI);        down.setAllowedOverRoaming(false);        MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();        String mimeString = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(url));        down.setMimeType(mimeString);        // 下载时,通知栏显示途中        down.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);        // 显示下载界面        down.setVisibleInDownloadsUi(true);        // 设置下载后文件存放的位置        down.setDestinationInExternalPublicDir(DOWNLOADPATH, "member.apk");        down.setTitle("member");        // 将下载请求放入队列        manager.enqueue(down);        //注册下载广播        registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        url = intent.getStringExtra("url");        String path = Environment.getExternalStorageDirectory().getAbsolutePath() +DOWNLOADPATH+ "member.apk";        File file = new File(path);        if (file.exists()) {            file.delete();        }        try {            // 调用下载            initDownManager();        } catch (Exception e) {            e.printStackTrace();            Toast.makeText(getApplicationContext(), "下载失败", Toast.LENGTH_SHORT).show();        }        return Service.START_NOT_STICKY;    }    @Override    public IBinder onBind(Intent intent) {        return null;    }    @Override    public void onDestroy() {        if (receiver != null)            // 注销下载广播            unregisterReceiver(receiver);        super.onDestroy();    }    // 接受下载完成后的intent    class DownloadCompleteReceiver extends BroadcastReceiver {        @Override        public void onReceive(Context context, Intent intent) {            //判断是否下载完成的广播            if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {                //获取下载的文件id                long downId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);                if (manager.getUriForDownloadedFile(downId) != null) {                    //自动安装apk                    installAPK(manager.getUriForDownloadedFile(downId), context);                    //installAPK(context);                } else {                    Toast.makeText(context, "下载失败", Toast.LENGTH_SHORT).show();                }                //停止服务并关闭广播                UpdataService.this.stopSelf();            }        }        private void installAPK(Uri apk, Context context) {            if (Build.VERSION.SDK_INT < 23) {                Intent intents = new Intent();                intents.setAction("android.intent.action.VIEW");                intents.addCategory("android.intent.category.DEFAULT");                intents.setType("application/vnd.android.package-archive");                intents.setData(apk);                intents.setDataAndType(apk, "application/vnd.android.package-archive");                intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                startActivity(intents);            } else {                File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +DOWNLOADPATH+ "member.apk");                if (file.exists()) {                    openFile(file, context);                }            }        }        private void installAPK(Context context) {            File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +DOWNLOADPATH+ "member.apk");            if (file.exists()) {                openFile(file, context);            } else {                Toast.makeText(context, "下载失败", Toast.LENGTH_SHORT).show();            }        }    }    public void openFile(File file, Context context) {        Intent intent = new Intent();        intent.addFlags(268435456);        intent.setAction("android.intent.action.VIEW");        String type = getMIMEType(file);        intent.setDataAndType(Uri.fromFile(file), type);        try {            context.startActivity(intent);        } catch (Exception var5) {            var5.printStackTrace();            Toast.makeText(context, "没有找到打开此类文件的程序", Toast.LENGTH_SHORT).show();        }    }    public String getMIMEType(File var0) {        String var1 = "";        String var2 = var0.getName();        String var3 = var2.substring(var2.lastIndexOf(".") + 1, var2.length()).toLowerCase();        var1 = MimeTypeMap.getSingleton().getMimeTypeFromExtension(var3);        return var1;    }}

更多相关文章

  1. 也谈Android下一个apk安装多个程序入口图标
  2. Android2.0游戏开发实战宝典下载
  3. 给android studio安装新字体,如mac系统的monaco字体
  4. Android用Broadcast实现EventBus的功能和用法
  5. 移动互联网和Android给你带来的机会[轉]
  6. Android(安卓)Studio打不开,无法启动,无反应的解决方案
  7. Android学习记录(4)—在java中学习多线程下载的基本原理和基本用法
  8. android编程之广播机制详解
  9. Android学习系列(3)--App自动更新之自定义进度视图和内部存储

随机推荐

  1. android GoogleMap定位(二)
  2. android:MIME 参考手册
  3. 关于编译Android时出现Error 41的解决方
  4. ubuntu 9.04使用adb调试Android方法
  5. cordova build android提示No installed
  6. ym——Android从零开始(26)(设置全屏+横
  7. Android横竖屏切换问题总结
  8. Android第一行代码学习笔记Chapter5&6
  9. 包管理系统分析 包管理脚本 /system/bin/
  10. 注解Annotation解析