首先我们开看一下效果图:
首先我来说一下,实现这个效果的整体思路:
自定义一个dialog,当有新版的时候,弹出这个dialog,当点击立即更新时,去下载apk。
一、我们首先需要自定义一个dialog
public class VersionDialog extends Dialog {
private int layoutRes;//布局文件
private Context mCtx;
private TextView versNameTv;
private TextView contentTv;
public Button positiveBtn;
public Button negativeBtn;
public VersionDialog(Context context) {
super(context);
// TODO Auto-generated constructor stub
this.mCtx = context;
}
public VersionDialog(Context context, int resLayout) {
super(context);
this.mCtx = context;
this.layoutRes = resLayout;
}
public VersionDialog(Context context, int theme,int resLayout) {
super(context, theme);
// TODO Auto-generated constructor stub
this.mCtx = context;
this.layoutRes = resLayout;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(layoutRes);
versNameTv = (TextView) findViewById(R.id.umeng_update_vers_name);
contentTv = (TextView) findViewById(R.id.umeng_update_content);
positiveBtn = (Button) findViewById(R.id.umeng_update_id_ok);
negativeBtn = (Button) findViewById(R.id.umeng_update_id_cancel);
}
public void setValue(String name,String content,int flag){
versNameTv.setText("最新版本:"+name);
contentTv.setText(content);
if (flag == 1) {
negativeBtn.setVisibility(View.GONE);
}
}
}
二、在代码里引用布局,当点击更新时,就下载apk
private void versDialog(String name, String content) {
versDialg = new VersionDialog(mCtx, R.style.version_dialog, R.layout.umeng_update_dialog);
versDialg.setCancelable(false);
versDialg.show();
versDialg.setValue(name, content, updateFlag);
versDialg.positiveBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!"".equals(versUrl)) {
Intent intent = new Intent(mCtx, DownloadService.class);
intent.putExtra("apkUrl", versUrl);
startService(intent);
versDialg.dismiss();
} else {
Tools.toast(mCtx, "下载地址有误,请重新登录");
finish();
}
}
});
versDialg.negativeBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
versDialg.dismiss();
}
});
}
R.style.version_dialog的样式
<!-- 版本更新dialog -->
<style name="version_dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:background">@null</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowBackground"> @android:color/transparent </item>
</style>
<color name="transparent">#00000000</color>
三、我们来看一下DownloadService
public class DownloadService extends Service {
private DownloadManager dm;
private long enqueue;
private BroadcastReceiver receiver;
private String apkName = "";
private String apkUrl;
private File apkFile = null;
public DownloadService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Intent intent1 = new Intent(Intent.ACTION_VIEW);
//判断是否是AndroidN以及更高的版本
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
intent1.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(context, "com.sanji.sjmallapp.fileprovider",new File(apkFile.getPath() + File.separator + apkName));
intent1.setDataAndType(contentUri,"application/vnd.android.package-archive");
}else{
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent1.setDataAndType(Uri.fromFile(new File(apkFile.getPath() + File.separator + apkName)),
"application/vnd.android.package-archive");
}
startActivity(intent1);
stopSelf();
}
};
apkUrl = intent.getStringExtra("apkUrl");
registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
startDownload();
return Service.START_STICKY;
}
private void startDownload(){
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(apkUrl));
request.setMimeType("application/vnd.android.package-archive");
apkName = "商城 " + Tools.getCurrentTime() + ".apk";
apkFile = new File(SDCardUtil.getApkPath());
if (apkFile != null && apkFile.exists() && apkFile.isDirectory()){
for (File item : apkFile.listFiles()){
item.delete();
}
}else{
apkFile.mkdirs();
}
if(SDCardUtil.checkSdCard()){
//下载到SD卡
request.setDestinationInExternalPublicDir(SDCardUtil.FILEAPK,apkName);
}else{
//下载到内置目录
request.setDestinationInExternalFilesDir(MallApplication.getApplication(),SDCardUtil.FILEAPK,apkName);
}
enqueue = dm.enqueue(request);
}
@Override
public void onDestroy() {
unregisterReceiver(receiver);
super.onDestroy();
}
}
Tools.getApkPath(getApplicationContext()))方法是根据有没有SD卡,来确定存储路径
/**
* 检测Sdcard是否存在
*
* @return
*/
public static boolean isExitsSdcard() {
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
return true;
else
return false;
}
/**
* 获取APK路径
*
* @param context
* @return
*/
public static String getApkPath(Context context) {
String apkPath = "";
if (isExitsSdcard()) {
// SD卡
apkPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + Constants.apkPath;
} else {
// data
apkPath = context.getFilesDir().getAbsolutePath() + File.separator + Constants.apkPath;
}
return apkPath;
}
public static String rootPath = "ExampleOne";
// 存放apk
public static String apkPath = rootPath + File.separator + "apk";
这是调用安卓系统自带的DownLoadManager,下载速度一般自己封装的快。