Android简易的自动更新(通知栏+系统下载),包含7.0系统解析安装包失败的处理
来源:互联网 发布:江歌 刘鑫 知乎 编辑:程序博客网 时间:2024/05/17 22:53
项目需要添加自动更新功能,因为是挪用加整理的,所以记笔记方面以后使用
首先是UpdateService,这个是整个功能的核心代码,所有关于更新的处理都在这个代码中
package com.example.updata;import java.io.File;import android.app.DownloadManager;import android.app.Service;import android.content.BroadcastReceiver;import android.content.ContentResolver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.database.Cursor;import android.net.Uri;import android.os.Build;import android.os.Environment;import android.os.IBinder;import android.provider.MediaStore;import android.support.v4.content.FileProvider;import android.util.Log;public class UpdateService extends Service{ public UpdateService() { } /** 安卓系统下载类 **/ DownloadManager manager; /** 接收下载完的广播 **/ DownloadCompleteReceiver receiver; /** 初始化下载器 **/ private void initDownManager() { manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); receiver = new DownloadCompleteReceiver(); // 设置下载地址 String urlPath = "http://192.168.1.109/Updata.apk"; Uri parse = Uri.parse(urlPath); DownloadManager.Request down = new DownloadManager.Request(parse); // 设置允许使用的网络类型,这里是移动网络和wifi都可以 down.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI); // 下载时,通知栏显示途中 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { down.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE); } // 显示下载界面 down.setVisibleInDownloadsUi(true); // 设置下载后文件存放的位置 String apkName = parse.getLastPathSegment(); down.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, apkName); // 将下载请求放入队列 manager.enqueue(down); // 注册下载广播 registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // 调用下载 initDownManager(); return super.onStartCommand(intent, flags, startId); } @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); Log.d("kodulf", "id=" + downId); // 自动安装apk if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { Uri uriForDownloadedFile = manager.getUriForDownloadedFile(downId); Log.d("kodulf", "uri=" + uriForDownloadedFile); installApkNew(uriForDownloadedFile); } // 停止服务并关闭广播 UpdateService.this.stopSelf(); } } // 安装apk protected void installApkNew(Uri uri) { String fileName = getRealFilePath(getApplicationContext(), uri); if (fileName != null) { if (fileName.endsWith(".apk")) { if (Build.VERSION.SDK_INT >= 24) {// 判读版本是否在7.0以上 File file = new File(fileName); Uri apkUri = FileProvider.getUriForFile(getApplicationContext(), "com.example.updata.fileprovider", file);// 在AndroidManifest中的android:authorities值 Intent install = new Intent(Intent.ACTION_VIEW); install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);// 添加这一句表示对目标应用临时授权该Uri所代表的文件 install.setDataAndType(apkUri, "application/vnd.android.package-archive"); getApplicationContext().startActivity(install); } else { Intent install = new Intent(Intent.ACTION_VIEW); install.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive"); install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); getApplicationContext().startActivity(install); } } } } /** * 通过Uri得到文件路径 * @param context * @param uri * @return */ protected String getRealFilePath(final Context context, final Uri uri) { if (null == uri) return null; final String scheme = uri.getScheme(); String data = null; if (scheme == null) data = uri.getPath(); else if (ContentResolver.SCHEME_FILE.equals(scheme)) { data = uri.getPath(); } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) { Cursor cursor = context.getContentResolver().query(uri, new String[] { MediaStore.Images.ImageColumns.DATA }, null, null, null); if (null != cursor) { if (cursor.moveToFirst()) { int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); if (index > -1) { data = cursor.getString(index); } } cursor.close(); } } return data; } }}
MainActivity
package com.example.updata;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** * * @param view */ public void update(View view) { new Thread(new Runnable() { @Override public void run() { // 启动服务 Intent service = new Intent(MainActivity.this, UpdateService.class); startService(service); } }).start(); }}
activity_main.xml布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:onClick="update" android:text="开始更新" android:textColor="@android:color/holo_red_dark" /></RelativeLayout>
AndroidManifest.xml中,为更新添加权限,以及注册服务
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<service android:name=".UpdateService" android:enabled="true" android:exported="true" > </service>
关于7.0的处理,首先,在AndroidManifest.xml中添加provider
<provider android:name="android.support.v4.content.FileProvider" android:authorities="com.example.updata.fileprovider" android:exported="false" android:grantUriPermissions="true" > <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /></provider>
在res/xml中添加file_paths.xml文件
<?xml version="1.0" encoding="utf-8"?><resources> <paths> <external-path name="download" path="" /> </paths></resources>
在UpdateService中,installApkNew方法安装APK,通过判断,处理7.0的安装
// 安装apk protected void installApkNew(Uri uri) { String fileName = getRealFilePath(getApplicationContext(), uri); if (fileName != null) { if (fileName.endsWith(".apk")) { if (Build.VERSION.SDK_INT >= 24) {// 判读版本是否在7.0以上 File file = new File(fileName); Uri apkUri = FileProvider.getUriForFile(getApplicationContext(), "com.example.updata.fileprovider", file);// 在AndroidManifest中的android:authorities值 Intent install = new Intent(Intent.ACTION_VIEW); install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);// 添加这一句表示对目标应用临时授权该Uri所代表的文件 install.setDataAndType(apkUri, "application/vnd.android.package-archive"); getApplicationContext().startActivity(install); } else { Intent install = new Intent(Intent.ACTION_VIEW); install.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive"); install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); getApplicationContext().startActivity(install); } } } }
这个方法里调用getRealFilePath方法来通过传递的Uri得到apk的文件路径
protected String getRealFilePath(final Context context, final Uri uri) { if (null == uri) return null; final String scheme = uri.getScheme(); String data = null; if (scheme == null) data = uri.getPath(); else if (ContentResolver.SCHEME_FILE.equals(scheme)) { data = uri.getPath(); } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) { Cursor cursor = context.getContentResolver().query(uri, new String[] { MediaStore.Images.ImageColumns.DATA }, null, null, null); if (null != cursor) { if (cursor.moveToFirst()) { int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); if (index > -1) { data = cursor.getString(index); } } cursor.close(); } } return data; }
这样,一个简易的自动更新就写出来了。
阅读全文
0 0
- Android简易的自动更新(通知栏+系统下载),包含7.0系统解析安装包失败的处理
- Linux系统的简易安装
- 【Android】App自动更新之通知栏下载
- Android 自定义系统通知栏的颜色
- Android顶部通知栏和系统通知栏的兼容问题
- 下载安装android系统的官方网站
- android之apk自动更新解析包失败问题
- Android系统通知处理流程
- 安装KB3135173后出现,Win10系统爆发式推送重置默认应用失败通知的问题
- Android 使用动画效果后的控件位置处理 类似系统通知栏下拉动画!!
- 解决APK下载到Cache目录安装提示“解析安装包失败”的问题
- ubuntu系统的自动更新方法
- U盘安装系统的简易教程
- U盘安装系统的简易教程
- Android 通过蒲公英pgyer的接口 Service 实现带进度下载App 通知栏显示 在线更新 自动更新Demo
- Android 通过蒲公英pgyer的接口 Service 实现带进度下载App 通知栏显示 在线更新 自动更新Demo
- Android学习系列(2)--App自动更新之通知栏下载
- Android学习系列(2)--App自动更新之通知栏下载
- quartz定时任务中常用的cron表达式
- 搭建Git服务器
- more than one device and emulator错误
- 周志华《Machine Learning》学习笔记(17)--强化学习
- 用最简单的过程讲述RNNs与LSTM的故事
- Android简易的自动更新(通知栏+系统下载),包含7.0系统解析安装包失败的处理
- java结构型设计模式——桥接模式
- TV Input Framework --Android官方说明
- 树莓派开端,开发环境配置
- 相隔、相差几天--算法
- Eccharts中自定义图表的绘制要点
- JMeter和JMeterPlugin的下载安装
- qiu969217118开通了他的C博客
- Django修改models