标签:
思路:
从服务器上拿到最新版本号,当前版本号。 比较版本号
拿到下载地址,进行下载(后台线程:例如 intentservice ,asyntack , hanlder )
进行安装( broadcastreciver上操作 )
String newAppUrl=“”; //app下载链接
private ProgressDialog progressdialog ;
//比较版本,及发送更新信息
public void compareVerson(){
progressdialog=ProgressDialog.show(this,"版本信息", "正在检查版本...");
progressdialog.setCancelable(true);
new Thread(new Runnable() {
@Override
public void run() {
int newVerson=getNewVerson( ); / /获取最新版本号,getNewVerson()没有贴出
int currentVerson=getVersonCode();
Looper.prepare();
Message msg=handler.obtainMessage();
if(newVerson>=currentVerson+1){ //存在最新版本
msg.arg1=2;
}
else{ //无最新版本
msg.arg1=0;
}
handler.sendMessage(msg);
Looper.loop();
}
}).start();
}
//获取版本号
public int getVersonCode(){
int versonCode;
try {
PackageInfo packageInfo= getPackageManager().getPackageInfo(getPackageName(), 0);
versonCode= packageInfo.versionCode;
} catch (Exception e) {
versonCode= 0 ;
e.printStackTrace();
}
return versonCode;
}
private Handler handler = new Handler(new android.os.Handler.Callback() {
public boolean handleMessage(Message msg) {
switch (msg.arg1) {
case 0: //对无最新版本的处理
progressdialog.dismiss();
Toast.makeText(LoginActivity.this,"暂无最新版本", Toast.LENGTH_SHORT).show();
break;
case 2:
progressdialog.dismiss();
new AlertDialog.Builder(LoginActivity.this)
.setTitle("在WIFI下进行更新?")
.setPositiveButton("确定", new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
new MyAsynTask().execute(newAppUrl);
arg0.dismiss();
} })
.setNegativeButton("取消", null)
.create().show();
break;
default:
break;
}
if (progressdialog != null) {
if (progressdialog.isShowing()) {
progressdialog.dismiss();
}
}
return false;
}
});
//在asytask中下载app
public class MyAsynTask extends AsyncTask<String,Void,Boolean>{
String weburi;
//该方法在工作线程中被调用。在工作线程进行下载 操作
protected Boolean doInBackground(String... arg0) {
weburi=arg0[0];
try {
downloadAPP(weburi);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true ;
}
@Override
protected void onPostExecute(Boolean result) {
if(!result){
Toast.makeText(LoginActivity.this,"下载失败", Toast.LENGTH_SHORT).show();
}
super.onPostExecute(result);
}
}
public static long myDownloadReference; //myDownloadReference变量是当前请求的id
//下载app,记录app所在路径
public void downloadAPP(String uri){
try {
DownloadManager manager=(DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Request request=new Request(Uri.parse(uri));
request.setTitle("ndfixedassets.apk"); //文件下载时显示的标题
request.setNotificationVisibility(Request.VISIBILITY_VISIBLE); //下载完成后,notification会自动消失
//运用在sdcard中专用文件
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "xxx.apk" );
myDownloadReference=manager.enqueue(request); //myDownloadReference变量是当前请求的id
} catch (Exception e) {
e.printStackTrace();
}
}
public class InstallAppReciver extends BroadcastReceiver {
//安装下载好后的app
public void onReceive( final Context context, Intent intent) {
long reference =intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if(LoginActivity.myDownloadReference==reference){ //接受到下载完最新该app后的操作
AlertDialog dialog= new AlertDialog.Builder(context)
.setTitle("安装新版本")
.setMessage("覆盖旧版本")
.setPositiveButton("确定",new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
install(context);
} })
.setNegativeButton("取消", null)
.create();
//设置dialog的类型确保,能在broadcastreceiver中正确弹出。
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.show();
}
}
public void install(Context context){
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Intent installintent =new Intent();
installintent.setAction(Intent.ACTION_VIEW);
//在Boradcast中启动活动需要添加Intent.FLAG_ACTIVITY_NEW_TASK
installintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
File file=new File(Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_DOWNLOADS), "xxx.apk");
installintent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
context.startActivity(installintent);
}else{
Toast.makeText(context, "内存卡不存在或者不可用", Toast.LENGTH_SHORT).show();
}
}
}
//联网前先检查网络情况,在这里,就不贴出来
<receiver
android:name="com.nandafixedassets.broadcastreceiver.InstallAppReciver">
<intent-filter
android:priority="100">
<action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
</intent-filter>
</receiver>
<!-- 系统级别的对话框的权限 -->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<!-- 连接网络的权限 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- 检查网络信息的权限 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- 在sdcard中写入权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
标签:
原文地址:http://my.oschina.net/u/2406195/blog/493563