android apk 升级代码
在闪屏中处理升级。
加入两项权限:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
style.xml中加入以下代码,去掉标题栏
<!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> <item name="android:windowNoTitle">true</item> </style>
SplashActivity:
package com.mhm.mySport; import java.io.File; import org.apache.http.Header; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import com.lidroid.xutils.HttpUtils; import com.lidroid.xutils.exception.HttpException; import com.lidroid.xutils.http.ResponseInfo; import com.lidroid.xutils.http.callback.RequestCallBack; import com.loopj.android.http.AsyncHttpClient; import com.loopj.android.http.JsonHttpResponseHandler; import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.TextView; import android.widget.Toast; public class SplashActivity extends Activity { TextView tv_version; TextView tv_progress; String server_v_name; int server_v_code; String server_v_url; String server_v_text; @SuppressLint("HandlerLeak") Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { if(msg.what == 1) { // 可升级 uploadDailog(); } else if(msg.what == 2) { // Toast.makeText(SplashActivity.this, "当前已是最新版本", Toast.LENGTH_SHORT).show(); enterHome(); } else { Toast.makeText(SplashActivity.this, "升级时遇到故障", Toast.LENGTH_SHORT).show(); } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); tv_version = (TextView) findViewById(R.id.tv_version); tv_version.setText("版本号:" + getLocalVersionName()); tv_progress= (TextView) findViewById(R.id.tv_progress); checkVersion(); } private void download() { // 判断是否有SD卡且SD卡挂载 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { tv_progress.setVisibility(View.VISIBLE); String target = Environment.getExternalStorageDirectory() + "/update.apk"; HttpUtils hu = new HttpUtils(); hu.download(server_v_url, target, new RequestCallBack<File>() { @Override public void onLoading(long total, long current, boolean isUploading) { super.onLoading(total, current, isUploading); tv_progress.setText("下载进度:" + current / total * 100 + "%"); } @Override public void onSuccess(ResponseInfo<File> arg0) { //打开安装界面 Intent intent = new Intent(Intent.ACTION_VIEW); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.setDataAndType(Uri.fromFile(arg0.result), "application/vnd.android.package-archive"); // 4.0及后续版本,需加此行代码,否则安装完后不进入APP界面 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } @Override public void onFailure(HttpException arg0, String arg1) { Toast.makeText(SplashActivity.this, "下载失败", Toast.LENGTH_SHORT).show(); } }); } else { Toast.makeText(SplashActivity.this, "没有SD卡", Toast.LENGTH_SHORT).show(); } } /** * 弹出是否更新对话框 */ private void uploadDailog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("最新版本:" + server_v_name); builder.setMessage(server_v_text); // 不升级不能使用该软件,尽量不用 // builder.setCancelable(false); builder.setPositiveButton("立即更新", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { download(); } }); builder.setNegativeButton("以后再说", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { enterHome(); } }); builder.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { enterHome(); } }); builder.show(); } private void enterHome() { Intent intent = new Intent(this, HomeActivity.class); startActivity(intent); finish(); } private String getLocalVersionName() { PackageManager packageManager = getPackageManager(); String versionName = "1.0"; //int versionCode = 1; try { PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), 0); versionName = packageInfo.versionName; //versionCode = packageInfo.versionCode; } catch (NameNotFoundException e) { e.printStackTrace(); } return versionName; } private int getLocalVersionCode() { PackageManager packageManager = getPackageManager(); int versionCode = 1; try { PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), 0); versionCode = packageInfo.versionCode; } catch (NameNotFoundException e) { e.printStackTrace(); } return versionCode; } private void checkVersion() { AsyncHttpClient client = new AsyncHttpClient(); String url = "http://58.216.180.114:7074/upload.json"; client.get(url, new JsonHttpResponseHandler(){ @Override public void onSuccess(int statusCode, Header[] headers, JSONObject response) { super.onSuccess(statusCode, headers, response); if (statusCode == 200) { //遍历json数组 try { // 获取具体的一个JSONObject对象 JSONObject obj = response; server_v_name = obj.getString("versionName"); server_v_code = obj.getInt("versionCode"); server_v_text = obj.getString("versionText"); server_v_url = obj.getString("url"); Message msg = new Message(); if(getLocalVersionCode() < server_v_code) { msg.what = 1; } else { msg.what = 2; } handler.sendMessage(msg); } catch (JSONException e) { e.printStackTrace(); } } } @Override public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) { super.onFailure(statusCode, headers, responseString, throwable); System.out.println("1"); } @Override public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) { super.onFailure(statusCode, headers, throwable, errorResponse); System.out.println("2"); } @Override public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) { super.onFailure(statusCode, headers, throwable, errorResponse); Message msg = new Message(); msg.what = 0; handler.sendMessage(msg); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { enterHome(); super.onActivityResult(requestCode, resultCode, data); } }
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28