Android 版本更新下载自动安装APK,并解决Android6.0安装失败的问题

来源:互联网 发布:奥斯卡软件 编辑:程序博客网 时间:2024/05/16 11:21

现在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{//提示没有权限,安装不了咯            }         }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

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;    }}
http://blog.csdn.net/asialyf/article/details/53259579
0 0
原创粉丝点击