Androidバージョン更新
36618 ワード
MainActivity.xml
Api
appservice
Bean
model
presenter
view
MainActivity
アクセス権
xml version="1.0" encoding="utf-8"?>
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="com.bwei.versionupdata.MainActivity">
android:padding="10dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:id="@+id/tv_versionName"
android:text=" 1.0 "
/>
Api
public class Api {
public static String URL="https://www.zhaoapi.cn/version/";
}
appservice
import retrofit2.http.GET;
import retrofit2.http.Query;
import rx.Observable;
/**
* Created by lenovo on 2018/4/20.
*/
public interface AppService {
@GET("getVersion")
Observable getIBean(@Query("type") String type);
}
Bean
public class IBean {
/**
* code : 0
* data : {"apkUrl":"https://www.zhaoapi.cn/version/101.apk","type":0,"vId":1,"versionCode":"101","versionName":"1.0.1"}
* msg :
*/
private String code;
private DataBean data;
private String msg;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public DataBean getData() {
return data;
}
public void setData(DataBean data) {
this.data = data;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public static class DataBean {
/**
* apkUrl : https://www.zhaoapi.cn/version/101.apk
* type : 0
* vId : 1
* versionCode : 101
* versionName : 1.0.1
*/
private String apkUrl;
private int type;
private int vId;
private String versionCode;
private String versionName;
public String getApkUrl() {
return apkUrl;
}
public void setApkUrl(String apkUrl) {
this.apkUrl = apkUrl;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public int getVId() {
return vId;
}
public void setVId(int vId) {
this.vId = vId;
}
public String getVersionCode() {
return versionCode;
}
public void setVersionCode(String versionCode) {
this.versionCode = versionCode;
}
public String getVersionName() {
return versionName;
}
public void setVersionName(String versionName) {
this.versionName = versionName;
}
}
}
model
public class IModel {
public interface OnListener{
void OnSucces(IBean iBean);
}
OnListener onListener;
public void setOnListener(OnListener onListener){
this.onListener=onListener;
}
public void getModel(String type){
Retrofit retrofit=new Retrofit.Builder().baseUrl(Api.URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
AppService appService = retrofit.create(AppService.class);
Observable iBean = appService.getIBean(type);
iBean.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(IBean iBean) {
if(onListener!=null){
onListener.OnSucces(iBean);
}
}
});
}
}
presenter
public class IPresenter {
IModel iModel;
IView iView;
public IPresenter(IView iView) {
this.iView = iView;
iModel=new IModel();
}
public void getPresenter(String type){
iModel.getModel(type);
iModel.setOnListener(new IModel.OnListener() {
@Override
public void OnSucces(IBean iBean) {
iView.OnSucces(iBean);
}
});
}
}
view
public interface IView {
void OnSucces(IBean iBean);
}
MainActivity
public class MainActivity extends AppCompatActivity implements IView {
/**
*
*/
private TextView mTvVersionName;
private IBean.DataBean data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
IPresenter iPresenter = new IPresenter(this);
iPresenter.getPresenter("2");
}
@Override
public void OnSucces(IBean iBean) {
data = iBean.getData();
}
private String getVersionName() throws Exception {
// packagemanager
PackageManager packageManager = getPackageManager();
//getPackageName() ,0
PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(), 0);
Log.e("TAG", " " + packInfo.versionCode);
Log.e("TAG", " " + packInfo.versionName);
return packInfo.versionName;
}
private int getVersionCode() throws Exception {
// packagemanager
PackageManager packageManager = getPackageManager();
//getPackageName() ,0
PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(), 0);
Log.e("TAG", " " + packInfo.versionCode);
Log.e("TAG", " " + packInfo.versionName);
return packInfo.versionCode;
}
private void initView() {
mTvVersionName = (TextView) findViewById(R.id.tv_versionName);
}
public void checkVersion(View view) throws Exception {
// ,
if (getVersionCode() < Integer.valueOf(data.getVersionCode())) {
showDialogUpdate();//
} else {
// ,
Toast.makeText(this, " ", Toast.LENGTH_SHORT).show();
}
}
private void showDialogUpdate() {
// , builder
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//
builder.setTitle(" ").
//
setIcon(R.mipmap.ic_launcher).
//
setMessage(" ! ").
//
setPositiveButton(" ", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Toast.makeText(MainActivity.this, " ", 0).show();
loadNewVersionProgress();//
}
}).
// ,null ,
setNegativeButton(" ", null);
//
AlertDialog alertDialog = builder.create();
//
alertDialog.show();
}
private void loadNewVersionProgress() {
// final String uri="http://www.apk.anzhi.com/data3/apk/201703/14/4636d7fce23c9460587d602b9dc20714_88002100.apk";
final ProgressDialog pd; //
pd = new ProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage(" ");
pd.show();
//
new Thread() {
@Override
public void run() {
try {
File file = getFileFromServer(data.getApkUrl(), pd);
sleep(3000);
installApk(file);
pd.dismiss(); //
} catch (Exception e) {
// apk
Toast.makeText(getApplicationContext(), " ", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}.start();
}
public static File getFileFromServer(String uri, ProgressDialog pd) throws Exception {
// sdcard
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
URL url = new URL(uri);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
//
pd.setMax(conn.getContentLength());
InputStream is = conn.getInputStream();
long time = System.currentTimeMillis();//
File file = new File(Environment.getExternalStorageDirectory(), time + "updata.apk");
FileOutputStream fos = new FileOutputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
byte[] buffer = new byte[1024];
int len;
int total = 0;
while ((len = bis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
total += len;
//
pd.setProgress(total);
}
fos.close();
bis.close();
is.close();
return file;
} else {
return null;
}
}
protected void installApk(File file) {
Intent intent = new Intent();
//
intent.setAction(Intent.ACTION_VIEW);
//
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
startActivity(intent);
}
}
アクセス権
android:name="android.permission.INTERNET"/>
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>