其实很简单,只需要几行代码就好了,首先要到服务器下载apk,然后才能安装,当然不是傻子应该都知道,我这里用到的是Httputils去下载,
这里需要一些权限
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <uses-permission android:name="android.permission.INSTALL_PACKAGES"/>
直接贴代码
package com.exmple.httpxutil; import java.io.File; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.Signature; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ProgressBar; import com.lidroid.xutils.HttpUtils; import com.lidroid.xutils.exception.HttpException; import com.lidroid.xutils.http.HttpHandler; import com.lidroid.xutils.http.ResponseInfo; import com.lidroid.xutils.http.callback.RequestCallBack; public class MainActivity extends Activity { HttpHandler<File> h; private ProgressBar pa; @SuppressLint("SdCardPath") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.button); pa = (ProgressBar) findViewById(R.id.pro); Button more = (Button) findViewById(R.id.more); getCode(); more.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent ints = new Intent(MainActivity.this, NewActivity.class); startActivity(ints); } }); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { HttpUtils http = new HttpUtils(); final String path = Environment.getExternalStorageDirectory() .getPath(); System.out.println(path); h = http.download( "http://101.200.142.201:8080/tqyb/baidumap.apk", path + "/badumap.apk", true, true, new RequestCallBack<File>() { @Override public void onStart() { System.out.println("==="); System.out.println("开始下载了++++++++++++++"); } @Override public void onLoading(long total, long current, boolean isUploading) { System.out.println(total + "====="); System.out.println(current + "====="); System.out.println(isUploading + "====="); pa.setMax((int) total); pa.setProgress((int) current); } @Override public void onFailure(HttpException error, String msg) { System.out.println(error + "+++++++" + msg + "+++++++++"); } @Override public void onSuccess( ResponseInfo<File> responseInfo) { h.cancel(); installApk(path + "/badumap.apk"); } }); } }); } //安装的方法 private void installApk(String filename) { File file = new File(filename); Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(Intent.ACTION_VIEW); String type = "application/vnd.android.package-archive"; intent.setDataAndType(Uri.fromFile(file), type); startActivity(intent); } //等到版本号的代码 private void getCode() { PackageManager manager; PackageInfo info = null; manager = this.getPackageManager(); try { info = manager.getPackageInfo(this.getPackageName(), 0); String name = info.versionName; int versionCode = info.versionCode; String packageName = info.packageName; Signature[] signatures = info.signatures; System.out.println(name + "++++++++++" + versionCode + "+++++" + packageName + "========nnn" + signatures + "============="); /* * info.versionName; * * info.packageName; * * info.signatures; */ } catch (NameNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
时间: 2024-10-09 06:27:45