Android如何更新app的版本
2015-05-11 15:18
337 查看
package com.runye.express.service; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Intent; import android.net.Uri; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.widget.RemoteViews; import com.runye.express.android.R; import com.runye.express.utils.FileUtils; import com.runye.express.utils.LogUtil; /*** * s 更新版本 * * @author zhangjia * */ public class UpdateService extends Service { private final String TAG = "UpdateService"; /** 超时 */ private static final int TIMEOUT = 10 * 1000; /** 下载地址 */ private static final String down_url = "http://www.tyfind.com:8080/find-consumers-android-update/new/find-consumers-android.apk"; /** 下载成功 */ private static final int DOWN_OK = 1; /** 下载失败 */ private static final int DOWN_ERROR = 0; /*** * 创建通知栏 */ RemoteViews mViews; /** 应用名称 */ private String app_name; /** 通知管理器 */ private NotificationManager notificationManager; /** 通知 */ private Notification notification; /** 点击通知跳转 */ private Intent mUpdateIntent; /** 等待跳转 */ private PendingIntent mPendingIntent; /** 通知ID */ private final int notification_id = 0; @Override public IBinder onBind(Intent arg0) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { LogUtil.d(TAG, "UpdateService+onStartCommand"); app_name = getResources().getString(R.string.app_name); // 创建文件 FileUtils.createFile(app_name); createNotification(); createThread(); return super.onStartCommand(intent, flags, startId); } /** * * @Description: 创建通知 * @return void */ public void createNotification() { notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); android.app.Notification.Builder builder = new Notification.Builder(this); mViews = new RemoteViews(getPackageName(), R.layout.notification_item); mViews.setImageViewResource(R.id.notificationImage, R.drawable.ic_launcher); mViews.setTextViewText(R.id.notificationTitle, "Find物流版正在下载"); mViews.setTextViewText(R.id.notificationPercent, "0%"); mViews.setProgressBar(R.id.notificationProgress, 100, 0, false); builder.setContent(mViews); mUpdateIntent = new Intent(Intent.ACTION_MAIN); mUpdateIntent.addCategory(Intent.CATEGORY_HOME); mPendingIntent = PendingIntent.getActivity(this, 0, mUpdateIntent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(mPendingIntent); builder.setTicker("开始下载,点击可查看"); builder.setSmallIcon(R.drawable.ic_launcher).setWhen(System.currentTimeMillis()).setAutoCancel(true);// 设置可以清除 notification = builder.getNotification(); notificationManager.notify(notification_id, notification); } /*** * 开线程下载 */ public void createThread() { /*** * 更新UI */ final Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case DOWN_OK: InstallationAPK(); break; case DOWN_ERROR: break; default: stopSelf(); break; } } }; final Message message = new Message(); new Thread(new Runnable() { @Override public void run() { try { long downloadSize = downloadUpdateFile(down_url, FileUtils.updateFile.toString()); if (downloadSize > 0) { // 下载成功 message.what = DOWN_OK; handler.sendMessage(message); } } catch (Exception e) { e.printStackTrace(); message.what = DOWN_ERROR; handler.sendMessage(message); } } }).start(); } /** * * @Description: 自动安装 * @return void */ private void InstallationAPK() { notificationManager.cancel(notification_id); // 停止服务 stopSelf(); // 下载完成,点击安装 Uri uri = Uri.fromFile(FileUtils.updateFile); Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(uri, "application/vnd.android.package-archive"); startActivity(intent); } /*** * 下载文件 * * @return * @throws MalformedURLException */ public long downloadUpdateFile(String down_url, String file) throws Exception { int down_step = 5;// 提示step int totalSize;// 文件总大小 int downloadCount = 0;// 已经下载好的大小 int updateCount = 0;// 已经上传的文件大小 InputStream inputStream; OutputStream outputStream; URL url = new URL(down_url); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setConnectTimeout(TIMEOUT); httpURLConnection.setReadTimeout(TIMEOUT); // 获取下载文件的size totalSize = httpURLConnection.getContentLength(); if (httpURLConnection.getResponseCode() == 404) { throw new Exception("fail!"); } inputStream = httpURLConnection.getInputStream(); outputStream = new FileOutputStream(file, false);// 文件存在则覆盖掉 byte buffer[] = new byte[1024]; int readsize = 0; while ((readsize = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, readsize); downloadCount += readsize;// 时时获取下载到的大小 /** * 每次增张5% */ if (updateCount == 0 || (downloadCount * 100 / totalSize - down_step) >= updateCount) { updateCount += down_step; mViews.setTextViewText(R.id.notificationPercent, updateCount + "%"); mViews.setProgressBar(R.id.notificationProgress, 100, updateCount, false); notificationManager.notify(notification_id, notification); } } if (httpURLConnection != null) { httpURLConnection.disconnect(); } inputStream.close(); outputStream.close(); return downloadCount; } } 复制代码 在需要用的地方,比如登陆的时候检查服务器与本地的版本 private void checkVersion() { if (NetWork.isNetworkConnected(LoginActivity.this)) { LogUtil.d(TAG, "开始检测更新\n"); if (MyApplication.getInstance().localVersion < MyApplication.getInstance().serverVersion) { LogUtil.d(TAG, "有新版本,开始更新\n" + "本地version:" + MyApplication.getInstance().localVersion + "\n服务器version:" + MyApplication.getInstance().serverVersion); // 发现新版本,提示用户更新 AlertDialog.Builder alert = new AlertDialog.Builder(LoginActivity.this); alert.setTitle("软件升级").setMessage("发现新版本,建议立即更新使用.") .setPositiveButton("更新", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 开启更新服务UpdateService updateIntent = new Intent(LoginActivity.this, UpdateService.class); startService(updateIntent); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); alert.create().show(); } else { LogUtil.d(TAG, "没有新版本,无需更新"); } } } 复制代码 |
相关文章推荐
- Android如何更新app的版本(中级)
- Android如何更新app的版本(中级)
- Android如何更新app的版本(中级)
- Android 如何通知用户更新app的版本
- Android如何更新app的版本(中级)
- Android 如何通知用户更新app的版本
- Cordova Android app版本更新(最新)
- android APP检测版本更新以及后台服务更新
- Android App的版本升級(迭代/更新)
- Android中如何实现版本更新
- iOS APP如何实现版本检测更新
- 【ionic App问题总结系列】ionic 如何更新app版本
- VS2015 Xamarin使用经验3-更新版本打包发布Android APP
- android的APP自动更新程序,检测版本,然后下载安装,但app安装后不提示“完成,打开”?
- Android实战之app版本更新升级全文章(一)
- Android实战之app版本更新升级全文章(二)
- Android实战之app版本更新升级全文章(三)
- Android app更新版本
- Android 实现通知栏通知APP版本更新
- android实现app版本更新案例