在网上查询了下。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更新带进度条的通知栏的更多相关文章

  1. Android -- 自定义带进度条的按钮

    1. 实现了一个带进度条的按钮,完成后显示提示信息,并设置按钮为不可再次被点击

  2. 【Android】带进度条的WebView

    http://www.cnblogs.com/over140/archive/2013/03/07/2947721.html

  3. web app升级—带进度条的App自动更新

    带进度条的App自动更新,效果如下图所示:   技术:vue.vant-ui.5+ 封装独立组件AppProgress.vue: <template> <div> <va ...

  4. 025 Android 带进度条的对话框(ProgressDialog)

    1.ProgressDialog介绍 ProgressDialog可以在当前界面弹出一个置顶于所有界面元素的对话框,同样具有屏蔽其他控件的交互能力,用于提示用户当前操作正在运行,让用户等待: 2.应用 ...

  5. Android带进度条的文件上传,使用AsyncTask异步任务

    最近项目中要做一个带进度条的上传文件的功能,学习了AsyncTask,使用起来比较方便,将几个方法实现就行,另外做了一个很简单的demo,希望能对大家有帮助,在程序中设好文件路径和服务器IP即可. A ...

  6. Android 自学之进度条ProgressBar

    进度条(ProgressBar)也是UI界面中的一种非常使用的组件,通常用于向用户显示某个耗时完成的百分比.因此进度条可以动态的显示进度,因此避免长时间地执行某个耗时操作时,让用户感觉程序失去了响应, ...

  7. atitit.文件上传带进度条的实现原理and组件选型and最佳实践总结O7

    atitit.文件上传带进度条的实现原理and组件选型and最佳实践总结O7 1. 实现原理 1 2. 大的文件上传原理::使用applet 1 3. 新的bp 2 1. 性能提升---分割小文件上传 ...

  8. 自定义带进度条的WebView , 增加获取web标题和url 回掉

    1.自定义ProgressWebView package com.app.android05; import android.content.Context; import android.graph ...

  9. C# WPF 解压缩7zip文件 带进度条 sevenzipsharp

      vs2013附件 :http://download.csdn.net/detail/u012663700/7427461 C# WPF 解压缩7zip文件 带进度条 sevenzipsharp W ...

随机推荐

  1. 3d数学 7 矩阵

    7.1 矩阵-数学定义 在线性代数中, 矩阵就是以行和列形式组织的矩形数字块.矩阵是向量的数组. 7.1.1 矩阵的维度和记法 矩阵的维度被定义为它包含了多少行和多少列.一个\(r \times c\ ...

  2. CodeForces 680A&680B&680C&680D Round#356

    昨天晚上实在是=_=困...(浪了一天)就没有去打Codeforces 中午醒来看看题,还不太难. A题:模拟(水题 3minAC) // by Sirius_Ren #include <cst ...

  3. java.util.Arrays

    package com.etc.Arrays; import java.util.Arrays; public class TestArraysClass { public static void m ...

  4. Vue 函数

    1.转换为大写字符 .toUpperCase() 2.字符串反转  this.message = this.message.split('').reverse().join('') 3.从index开 ...

  5. bug,实现类未找到service

  6. GitHub上fork别人打代码后如何保持和原作者同步的更新

    1.进入你的GitHub发起Pull  request 2.选择compare across  forks 3.反向操作.base fork改为自己的,head fork改为原作者的 4.点击 cre ...

  7. linux 新添加的硬盘格式化并挂载到目录下

    需求: 新增加一块硬盘sdb,将sdb分区,只分一个区,格式化,挂载到目录/ssd下. 1.  查看现在已有的分区状态 # df –l 图中显示,没有看到sdb硬盘 2.  查看服务器安装的硬盘状态( ...

  8. SAP computer之input and MAR

    Input and MAR Below the program counter is the input and MAR block. It includes the address and data ...

  9. Mysql正则

    摘自:http://www.runoob.com/mysql/mysql-regexp.html 模式 描述 ^ 匹配输入字符串的开始位置.如果设置了 RegExp 对象的 Multiline 属性, ...

  10. BZOJ 3943: [Usaco2015 Feb]SuperBull 最小生成树

    Code: // luogu-judger-enable-o2 #include<bits/stdc++.h> #define setIO(s) freopen(s".in&qu ...