下載安裝Apk
阿新 • • 發佈:2019-02-11
這個demo主要是做判斷版本並在網上下載並安裝apk
需要匯入xutils3架包
Mainactivity(主介面 進行一些操作)
import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Environment; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.WebResourceRequest; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import org.xutils.common.Callback;import org.xutils.http.RequestParams; import org.xutils.x; import java.io.File; public class MainActivity extends AppCompatActivity { private int versionCode; private ProgressDialog progressDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); PackageManager packageManager = getPackageManager(); try { PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), 0); versionCode = packageInfo.versionCode; showUpdate(); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } } private void showUpdate() { new AlertDialog.Builder(this) .setTitle("更新版本") .setMessage("當前版本是" + versionCode + " 需要升級") .setPositiveButton("下載", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { downloadApk(); } }) .setNegativeButton("取消", null) .create() .show(); } private void downloadApk() { String url = "http://gdown.baidu.com/data/wisegame/f98d235e39e29031/baiduxinwen.apk"; String path = Environment.getExternalStorageDirectory().getPath() + "/teme1/myapk.apk"; File file = new File(path); File parentFile = file.getParentFile(); if (!parentFile.exists()) { parentFile.mkdir(); } RequestParams params = new RequestParams(url); params.setAutoRename(false); params.setAutoResume(true); //設定儲存路徑 params.setSaveFilePath(path); x.http().get(params, new Callback.ProgressCallback<File>() { @Override public void onSuccess(File result) { installDownloadApk(result); } @Override public void onError(Throwable ex, boolean isOnCallback) { } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { cancleProgressDialog(); } @Override public void onWaiting() { } @Override public void onStarted() { showProgressDialog(); } @Override public void onLoading(long total, long current, boolean isDownloading) { int progress = (int) (current / total * 100); if (progress >= 0 && progress <= 100) { updataProgressDialog(progress); } } }); } //安卓開發 apk安裝 private void installDownloadApk(File result) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(Uri.fromFile(result), "application/vnd.android.package-archive"); startActivity(intent); } private void cancleProgressDialog() { if (progressDialog == null) { return; } if (progressDialog.isShowing()) { progressDialog.dismiss(); } } private void updataProgressDialog(int progress) { if (progressDialog == null) { return; } progressDialog.setProgress(progress); } private void showProgressDialog() { progressDialog = new ProgressDialog(this); //設定progressDialog顯示樣式 progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setMessage("我正在下載東西"); progressDialog.setTitle("請等待"); progressDialog.setProgress(0); progressDialog.show(); } }
MyApplication(配置xutils3)
package com.example.lixin.downloadapkdemo.app; import android.app.Application; import org.xutils.x; /** * Created by hua on 2017/8/19. */ public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); x.Ext.init(this); } }
manifest(配置檔案)
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.lixin.downloadapkdemo"> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <applicationandroid:name=".app.MyApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>