Activity和广播
/**下载APK细节1、点击升级后对话框不消失,再次点击时不能重复下载2、下载过程中退出APP,下次进入应用后要重新下载(因为可能不完整)3、下载过程中退出APP(或下载完成后退出),如果APK已经下载完毕,则下次进入应用后不再下载4、如果下载的APK被清除了,要重新下载5、断网后重新连接或切换网络后不受影响*/public class MainActivity extends Activity {private TextView textView;private DownloadCompleteReceiver receiver = null;private String url = "http://113.107.216.43/imtt.dd.qq.com/16891/BD5BA98958C0FEC9D88FB65BF5534A3B.apk?mkey=581c007d0e1baac8&f=8a5d&c=0&fsname=com.bcb_2.0.2_52.apk&csr=4d5s&p=.apk";private String fileName;private AlertDialog dialog;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);textView = new TextView(this);setContentView(textView);fileName = "福利金融-" + Utiles.getVersionCode(this) + ".apk";textView.setText("文件名:" + fileName //+ "\n文件路径:" + Utiles.getApkFile(fileName).getPath() //+ "\n是否已下载过此版本的安装包:" + Utiles.getIsDownloaded(this, fileName)//+ "\n是否已下载时退出了:" + Utiles.getIsFinishedWhenDownloading(this));showVersionDialog();}private void showVersionDialog() {AlertDialog.Builder ibuilder = new AlertDialog.Builder(this);ibuilder.setTitle("版本更新啦!");ibuilder.setMessage("优化用户界面,界面更好看\n修复所有已经的bug");ibuilder.setNegativeButton("退出应用", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {dialog.dismiss();dialog = null;finish();}});ibuilder.setPositiveButton("立即更新", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {File file = Utiles.getApkFile(fileName);if (Utiles.getIsDownloaded(MainActivity.this, fileName)) {//已下载完毕if (file == null || !file.exists()) {Utiles.clearSP(MainActivity.this);//清空保存的两个数据registerReceiver();//被删了,重新下载} else Utiles.installApk(MainActivity.this, file);//否则,安装} else if (Utiles.getIsFinishedWhenDownloading(MainActivity.this)) {//上次在下载过程中退出了,下次进入应用时重新下载registerReceiver();//重新下载} else if (file != null && file.exists()) {//正在下载Toast.makeText(MainActivity.this, "正在下载,请稍后", Toast.LENGTH_SHORT).show();} else {//没下载过registerReceiver();}}});dialog = ibuilder.create();dialog.setCanceledOnTouchOutside(false);dialog.setCancelable(false);dialog.show();}private void registerReceiver() {receiver = new DownloadCompleteReceiver();IntentFilter intentFilter = new IntentFilter();intentFilter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);//下载完成的动作registerReceiver(receiver, intentFilter);Utiles.downLoadFile(MainActivity.this, url, fileName);//开始下载}class DownloadCompleteReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {Toast.makeText(MainActivity.this, "下载完毕", Toast.LENGTH_SHORT).show();textView.append("下载完毕");Utiles.saveIsDownloaded(context, fileName);Utiles.installApk(context, Utiles.getApkFile(fileName));if (receiver != null) {unregisterReceiver(receiver);receiver = null;}}}}@Overrideprotected void onDestroy() {super.onDestroy();if (receiver != null) {//如果receiver不为空,说明正在下载APK包,即使应用退出了,也会继续下载unregisterReceiver(receiver);Utiles.saveIsFinishedWhenDownloading(this);//如果在下载过程中退出了,下次进入应用时重新下载}}}
工具
public class Utiles {public static final String SP_NAME = "version";public static final String FILE_PATH = "/fljr/apk";/**返回下载的APK文件*/public static File getApkFile(String fileName) {String filePath = Environment.getExternalStorageDirectory().getPath() + FILE_PATH + File.separator + fileName;return new File(filePath);}/**安装指定的APK包*/public static void installApk(Context context, File apkFile) {Intent mIntent = new Intent(Intent.ACTION_VIEW);mIntent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);context.startActivity(mIntent);}/**获取VersionCode*/public static int getVersionCode(Context context) {try {PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);return packageInfo.versionCode;} catch (PackageManager.NameNotFoundException e) {e.printStackTrace();return 0;}}/**清空SP的数据*/
public static void clearSP(Context context) {SharedPreferences.Editor editor = context.getSharedPreferences(SP_NAME, 0).edit();editor.clear();editor.commit();}//******************************************************************************************/**记录是否在下载过程中退出了,如果在下载过程中退出了,下次进入应用时重新下载*/public static void saveIsFinishedWhenDownloading(Context context) {SharedPreferences.Editor editor = context.getSharedPreferences(SP_NAME, 0).edit();editor.putBoolean("finish", true);editor.commit();}/**是否在下载过程中退出了*/public static boolean getIsFinishedWhenDownloading(Context context) {return context.getSharedPreferences(SP_NAME, 0).getBoolean("finish", false);}//******************************************************************************************/**记录是否已下载过指定版本的APK包*/public static void saveIsDownloaded(Context context, String fileName) {SharedPreferences.Editor editor = context.getSharedPreferences(SP_NAME, 0).edit();editor.putBoolean(fileName, true);editor.commit();}/**查看是否已下载过指定版本的APK包*/public static boolean getIsDownloaded(Context context, String fileName) {return context.getSharedPreferences(SP_NAME, 0).getBoolean(fileName, false);}/**下载文件*/public static void downLoadFile(Context mContext, String url, String fileName) {DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url))//下载路径.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)//显示方式。默认,下载过程中显示,下载完成后自动消失.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)//网络设置.setDestinationInExternalPublicDir(FILE_PATH, fileName);//文件下载路径((DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE)).enqueue(request);}}