Android更新带进度条的通知栏
Posted cxchanpin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android更新带进度条的通知栏相关的知识,希望对你有一定的参考价值。
在网上查询了下,android版本号更新通知栏带进度条,醉了,基本都是复制过来。有的代码不全,连源代码下载都没有,有下载也须要积分。还不能用。真黑心啊!!
之前自己也写过自己定义通知栏Notification,想了还是自己写吧。
由于在通知栏更新,须要訪问网络下载,就写了个服务。在服务中实现了下载个更新。
先看MainActivity代码:
package com.wsj.wsjdemo; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initGlobal(); checkVersion(); } /** * 初始化全局变量 * 实际工作中这种方法中serverVersion从服务器端获取,最好在启动画面的activity中运行 */ public void initGlobal(){ try{ Global.localVersion = getPackageManager().getPackageInfo(getPackageName(),0).versionCode; //设置本地版本号号 Global.serverVersion = 2;//假定服务器版本号为2,本地版本号默认是1 }catch (Exception ex){ ex.printStackTrace(); } } /** * 检查更新版本号 */ public void checkVersion(){ if(Global.localVersion < Global.serverVersion){ //发现新版本号。提示用户更新 AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.setTitle("软件升级") .setMessage("发现新版本号,建议马上更新使用.") .setPositiveButton("更新", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //开启更新服务UpdateService //这里为了把update更好模块化,能够传一些updateService依赖的值 //如布局ID,资源ID。动态获取的标题,这里以app_name为例 Intent updateIntent =new Intent(MainActivity.this, UpdateService.class); updateIntent.putExtra("app_name",R.string.app_name); updateIntent.putExtra("downurl","http://www.subangloan.com/Contract/App/Android/caijia_unsign_signed.apk"); startService(updateIntent); } }) .setNegativeButton("取消",new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); alert.create().show(); }else{ //清理工作,略去 //cheanUpdateFile(),文章后面我会附上代码 } } }
activity_main.xml文件:什么都没有就一个简单界面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </LinearLayout>
在Main中的用到的Global.java
package com.wsj.wsjdemo; public class Global { //版本号信息 public static int localVersion = 0; public static int serverVersion = 0; public static String downloadDir = "app/download/"; }
写的服务实现UpdateService.java
package com.wsj.wsjdemo; import java.io.File; import java.io.IOException; import java.text.DecimalFormat; import com.lidroid.xutils.HttpUtils; import com.lidroid.xutils.exception.HttpException; import com.lidroid.xutils.http.ResponseInfo; import com.lidroid.xutils.http.callback.RequestCallBack; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.widget.RemoteViews; public class UpdateService extends Service { private static String down_url; // = "http://192.168.1.112:8080/360.apk"; private static final int DOWN_OK = 1; // 下载完毕 private static final int DOWN_ERROR = 0; private String app_name; private NotificationManager notificationManager; private Notification notification; private Intent updateIntent; private PendingIntent pendingIntent; private String updateFile; private int notification_id = 0; long totalSize = 0;// 文件总大小 /*** * 更新UI */ final Handler handler = new Handler() { @SuppressWarnings("deprecation") @Override public void handleMessage(Message msg) { switch (msg.what) { case DOWN_OK: // 下载完毕,点击安装 Intent installApkIntent = getFileIntent(new File(updateFile)); pendingIntent = PendingIntent.getActivity(UpdateService.this, 0, installApkIntent, 0); notification.contentIntent = pendingIntent; notification.flags |= Notification.FLAG_AUTO_CANCEL; notification.setLatestEventInfo(UpdateService.this, app_name, "下载成功,点击安装", pendingIntent); notificationManager.notify(notification_id, notification); stopService(updateIntent); break; case DOWN_ERROR: notification.setLatestEventInfo(UpdateService.this, app_name, "下载失败", pendingIntent); break; default: stopService(updateIntent); break; } } }; @Override public IBinder onBind(Intent arg0) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { if (intent != null) { try { app_name = intent.getStringExtra("app_name"); down_url = intent.getStringExtra("downurl"); // 创建文件 File updateFile = FileUtils.getDiskCacheDir(getApplicationContext(), "xxxx.apk"); if (!updateFile.exists()) { try { updateFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } // 创建通知 createNotification(); // 開始下载 downloadUpdateFile(down_url, updateFile.getAbsolutePath()); } catch (Exception e) { e.printStackTrace(); } } return super.onStartCommand(intent, flags, startId); } /*** * 创建通知栏 */ RemoteViews contentView; @SuppressWarnings("deprecation") public void createNotification() { notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notification = new Notification(); notification.icon = R.drawable.ic_launcher; // 这个參数是通知提示闪出来的值. notification.tickerText = "開始下载"; // pendingIntent = PendingIntent.getActivity(this, 0, updateIntent, 0); // 这里面的參数是通知栏view显示的内容 notification.setLatestEventInfo(this, app_name, "下载:0%", pendingIntent); // notificationManager.notify(notification_id, notification); /*** * 在这里我们用自定的view来显示Notification */ contentView = new RemoteViews(getPackageName(), R.layout.notification_item); contentView.setTextViewText(R.id.notificationTitle, "正在下载"); contentView.setTextViewText(R.id.notificationPercent, "0%"); contentView.setProgressBar(R.id.notificationProgress, 100, 0, false); notification.contentView = contentView; updateIntent = new Intent(this, MainActivity.class); updateIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); pendingIntent = PendingIntent.getActivity(this, 0, updateIntent, 0); notification.contentIntent = pendingIntent; notificationManager.notify(notification_id, notification); } /*** * 下载文件 */ public void downloadUpdateFile(String down_url, String file) throws Exception { updateFile = file; HttpUtils HttpUtils = new HttpUtils(); HttpUtils.download(down_url, file, new RequestCallBack<File>() { @Override public void onSuccess(ResponseInfo<File> responseInfo) { // 下载成功 Message message = handler.obtainMessage(); message.what = DOWN_OK; handler.sendMessage(message); installApk(new File(updateFile), UpdateService.this); } @Override public void onFailure(HttpException error, String msg) { Message message = handler.obtainMessage(); message.what = DOWN_ERROR; handler.sendMessage(message); } @Override public void onLoading(long total, long current, boolean isUploading) { super.onLoading(total, current, isUploading); double x_double = current * 1.0; double tempresult = x_double / total; DecimalFormat df1 = new DecimalFormat("0.00"); // ##.00% // 百分比格式。后面不足2位的用0补齐 String result = df1.format(tempresult); contentView.setTextViewText(R.id.notificationPercent, (int) (Float.parseFloat(result) * 100) + "%"); contentView.setProgressBar(R.id.notificationProgress, 100, (int) (Float.parseFloat(result) * 100), false); notificationManager.notify(notification_id, notification); } }); } // 下载完毕后打开安装apk界面 public static void installApk(File file, Context context) { //L.i("msg", "版本号更新获取sd卡的安装包的路径=" + file.getAbsolutePath()); Intent openFile = getFileIntent(file); context.startActivity(openFile); } public static Intent getFileIntent(File file) { Uri uri = Uri.fromFile(file); String type = getMIMEType(file); Intent intent = new Intent("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(uri, type); return intent; } public static String getMIMEType(File f) { String type = ""; String fName = f.getName(); // 取得扩展名 String end = fName .substring(fName.lastIndexOf(".") + 1, fName.length()); if (end.equals("apk")) { type = "application/vnd.android.package-archive"; } else { // /*假设无法直接打开。就跳出软件列表给用户选择 */ type = "*/*"; } return type; } }
基本上凝视写的清清楚楚,这当中用到了Xutils开源框架来下载文件。
如过你认为看的麻烦直接点击下载demo吧,不须要积分.。。。
以上是关于Android更新带进度条的通知栏的主要内容,如果未能解决你的问题,请参考以下文章