Android Apk自动更新service(直接拿来用)转载
2016-11-16 15:18
369 查看
转载自http://blog.csdn.net/evn512457/article/details/28626621
Android sdk
version 9以上就有DownManager
使用DownManager,我们可以很简便的在各个安卓机子上升级自家的应用
本例写了一个UpdataService实现后台下载新的APK到sdcard,并自动安装更新。
[java] view
plain copy
/**
* 检测安装更新文件的助手类
*
* @author G.Y.Y
*
*/
public class UpdataService extends Service {
/** 安卓系统下载类 **/
DownloadManager manager;
/** 接收下载完的广播 **/
DownloadCompleteReceiver receiver;
/** 初始化下载器 **/
private void initDownManager() {
manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
receiver = new DownloadCompleteReceiver();
//设置下载地址
DownloadManager.Request down = new DownloadManager.Request(
Uri.parse("http://gdown.baidu.com/data/wisegame/fd84b7f6746f0b18/baiduyinyue_4802.apk"));
// 设置允许使用的网络类型,这里是移动网络和wifi都可以
down.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE
| DownloadManager.Request.NETWORK_WIFI);
// 下载时,通知栏显示途中
down.setNotificationVisibility(Request.VISIBILITY_VISIBLE);
// 显示下载界面
down.setVisibleInDownloadsUi(true);
// 设置下载后文件存放的位置
down.setDestinationInExternalFilesDir(this,
Environment.DIRECTORY_DOWNLOADS, "baidumusic.apk");
// 将下载请求放入队列
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);
//自动安装apk
installAPK(manager.getUriForDownloadedFile(downId));
//停止服务并关闭广播
UpdataService.this.stopSelf();
}
}
/**
* 安装apk文件
*/
private void installAPK(Uri apk) {
// 通过Intent安装APK文件
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);
android.os.Process.killProcess(android.os.Process.myPid());
// 如果不加上这句的话在apk安装完成之后点击单开会崩溃
startActivity(intents);
}
}
}
AndroidManifest.xml注册service
[html] view
plain copy
<service
android:name="com.example.test.UpdataService"
android:enabled="true"
>
</service>
添加调用DownManager的权限
[html] view
plain copy
<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" />
Android sdk
version 9以上就有DownManager
使用DownManager,我们可以很简便的在各个安卓机子上升级自家的应用
本例写了一个UpdataService实现后台下载新的APK到sdcard,并自动安装更新。
[java] view
plain copy
/**
* 检测安装更新文件的助手类
*
* @author G.Y.Y
*
*/
public class UpdataService extends Service {
/** 安卓系统下载类 **/
DownloadManager manager;
/** 接收下载完的广播 **/
DownloadCompleteReceiver receiver;
/** 初始化下载器 **/
private void initDownManager() {
manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
receiver = new DownloadCompleteReceiver();
//设置下载地址
DownloadManager.Request down = new DownloadManager.Request(
Uri.parse("http://gdown.baidu.com/data/wisegame/fd84b7f6746f0b18/baiduyinyue_4802.apk"));
// 设置允许使用的网络类型,这里是移动网络和wifi都可以
down.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE
| DownloadManager.Request.NETWORK_WIFI);
// 下载时,通知栏显示途中
down.setNotificationVisibility(Request.VISIBILITY_VISIBLE);
// 显示下载界面
down.setVisibleInDownloadsUi(true);
// 设置下载后文件存放的位置
down.setDestinationInExternalFilesDir(this,
Environment.DIRECTORY_DOWNLOADS, "baidumusic.apk");
// 将下载请求放入队列
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);
//自动安装apk
installAPK(manager.getUriForDownloadedFile(downId));
//停止服务并关闭广播
UpdataService.this.stopSelf();
}
}
/**
* 安装apk文件
*/
private void installAPK(Uri apk) {
// 通过Intent安装APK文件
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);
android.os.Process.killProcess(android.os.Process.myPid());
// 如果不加上这句的话在apk安装完成之后点击单开会崩溃
startActivity(intents);
}
}
}
AndroidManifest.xml注册service
[html] view
plain copy
<service
android:name="com.example.test.UpdataService"
android:enabled="true"
>
</service>
添加调用DownManager的权限
[html] view
plain copy
<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" />
相关文章推荐
- Android Apk自动更新service(直接拿来用)
- Android Apk自动更新service(直接拿来用)
- Android Apk自动更新service(直接拿来用)
- android利用service实现下载apk自动更新功能
- Android版本更新(Service下载 Notification进度条:直接拿来用)
- Android客户端apk自动检测更新自动下载自动安装的实现方法
- Android 自动更新APK
- android 自动更新apk版本
- android 自动更新apk版本
- Android 一步一步实现版本自动更新(第二步 下载和安装apk)
- (转载)Android实现应用下载并自动安装apk包
- 转载__直接拿来用!最火的Android开源项目(一)
- Android 程序自动安装下载好的apk 、程序的安装、卸载和更新
- Android 代码自动升级新版本更新apk时出现“已安装了存在签名冲突的同名数据包”
- Android 通过蒲公英pgyer的接口 Service 实现带进度下载App 通知栏显示 在线更新 自动更新Demo
- Android 自动检测更新apk版本
- android之apk自动更新采用Notification通知提示并显示下载进度
- Android使用Service自动下载apk自动安装
- android之apk自动更新解析包失败问题
- android 自动更新apk版本