<p>Android Apk自动更新service</p>
<p>Android sdk version 9以上就有DownManager<br />
使用DownManager,我们可以很简便的在各个安卓机子上升级自家的应用<br />
本例写了一个UpdataService实现后台下载新的APK到sdcard,并自动安装更新。<br />
/**<br />
* 检测安装更新文件的助手类<br />
* <br />
* @author G.Y.Y<br />
* <br />
*/ <br />
public class UpdataService extends Service { <br />
/** 安卓系统下载类 **/ <br />
DownloadManager manager; <br />
/** 接收下载完的广播 **/ <br />
DownloadCompleteReceiver receiver; <br />
/** 初始化下载器 **/ <br />
private void initDownManager() { <br />
manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); <br />
receiver = new DownloadCompleteReceiver(); <br />
//设置下载地址 <br />
DownloadManager.Request down = new DownloadManager.Request( <br />
Uri.parse("http://gdown.baidu.com/data/wisegame/fd84b7f6746f0b18/baiduyinyue_4802.apk")); <br />
<br />
// 设置允许使用的网络类型,这里是移动网络和wifi都可以 <br />
down.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE <br />
| DownloadManager.Request.NETWORK_WIFI); <br />
<br />
// 下载时,通知栏显示途中 <br />
down.setNotificationVisibility(Request.VISIBILITY_VISIBLE); <br />
<br />
// 显示下载界面 <br />
down.setVisibleInDownloadsUi(true); <br />
<br />
// 设置下载后文件存放的位置 <br />
down.setDestinationInExternalFilesDir(this, <br />
Environment.DIRECTORY_DOWNLOADS, "baidumusic.apk"); <br />
<br />
// 将下载请求放入队列 <br />
manager.enqueue(down); <br />
<br />
//注册下载广播 <br />
registerReceiver(receiver, new IntentFilter( <br />
DownloadManager.ACTION_DOWNLOAD_COMPLETE)); <br />
} <br />
<br />
@Override <br />
public int onStartCommand(Intent intent, int flags, int startId) { <br />
<br />
// 调用下载 <br />
initDownManager(); <br />
<br />
return super.onStartCommand(intent, flags, startId); <br />
} <br />
<br />
@Override <br />
public IBinder onBind(Intent intent) { <br />
<br />
return null; <br />
} <br />
<br />
@Override <br />
public void onDestroy() { <br />
<br />
// 注销下载广播 <br />
if (receiver != null) <br />
unregisterReceiver(receiver); <br />
<br />
super.onDestroy(); <br />
} <br />
<br />
// 接受下载完成后的intent <br />
class DownloadCompleteReceiver extends BroadcastReceiver { <br />
<br />
@Override <br />
public void onReceive(Context context, Intent intent) { <br />
<br />
//判断是否下载完成的广播 <br />
if (intent.getAction().equals( <br />
DownloadManager.ACTION_DOWNLOAD_COMPLETE)) { <br />
<br />
//获取下载的文件id <br />
long downId = intent.getLongExtra( <br />
DownloadManager.EXTRA_DOWNLOAD_ID, -1); <br />
<br />
//自动安装apk <br />
installAPK(manager.getUriForDownloadedFile(downId)); <br />
<br />
//停止服务并关闭广播 <br />
UpdataService.this.stopSelf(); <br />
<br />
} <br />
} <br />
<br />
/**<br />
* 安装apk文件<br />
*/ <br />
private void installAPK(Uri apk) { <br />
<br />
// 通过Intent安装APK文件 <br />
Intent intents = new Intent(); <br />
<br />
intents.setAction("android.intent.action.VIEW"); <br />
intents.addCategory("android.intent.category.DEFAULT"); <br />
intents.setType("application/vnd.android.package-archive"); <br />
intents.setData(apk); <br />
intents.setDataAndType(apk,"application/vnd.android.package-archive"); <br />
intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); <br />
android.os.Process.killProcess(android.os.Process.myPid()); <br />
// 如果不加上这句的话在apk安装完成之后点击单开会崩溃 <br />
<br />
startActivity(intents); <br />
<br />
} <br />
<br />
} <br />
} </p>
<p>AndroidManifest.xml注册service<br />
<service android:name="com.example.test.UpdataService" <br />
android:enabled="true" <br />
> </service> </p>
<p>添加调用DownManager的权限<br />
<uses-permission android:name="android.permission.INTERNET" /> <br />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <br />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" /> </p>