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更新带进度条的通知栏的更多相关文章
- Android -- 自定义带进度条的按钮
1. 实现了一个带进度条的按钮,完成后显示提示信息,并设置按钮为不可再次被点击
- 【Android】带进度条的WebView
http://www.cnblogs.com/over140/archive/2013/03/07/2947721.html
- web app升级—带进度条的App自动更新
带进度条的App自动更新,效果如下图所示: 技术:vue.vant-ui.5+ 封装独立组件AppProgress.vue: <template> <div> <va ...
- 025 Android 带进度条的对话框(ProgressDialog)
1.ProgressDialog介绍 ProgressDialog可以在当前界面弹出一个置顶于所有界面元素的对话框,同样具有屏蔽其他控件的交互能力,用于提示用户当前操作正在运行,让用户等待: 2.应用 ...
- Android带进度条的文件上传,使用AsyncTask异步任务
最近项目中要做一个带进度条的上传文件的功能,学习了AsyncTask,使用起来比较方便,将几个方法实现就行,另外做了一个很简单的demo,希望能对大家有帮助,在程序中设好文件路径和服务器IP即可. A ...
- Android 自学之进度条ProgressBar
进度条(ProgressBar)也是UI界面中的一种非常使用的组件,通常用于向用户显示某个耗时完成的百分比.因此进度条可以动态的显示进度,因此避免长时间地执行某个耗时操作时,让用户感觉程序失去了响应, ...
- atitit.文件上传带进度条的实现原理and组件选型and最佳实践总结O7
atitit.文件上传带进度条的实现原理and组件选型and最佳实践总结O7 1. 实现原理 1 2. 大的文件上传原理::使用applet 1 3. 新的bp 2 1. 性能提升---分割小文件上传 ...
- 自定义带进度条的WebView , 增加获取web标题和url 回掉
1.自定义ProgressWebView package com.app.android05; import android.content.Context; import android.graph ...
- C# WPF 解压缩7zip文件 带进度条 sevenzipsharp
vs2013附件 :http://download.csdn.net/detail/u012663700/7427461 C# WPF 解压缩7zip文件 带进度条 sevenzipsharp W ...
随机推荐
- Java算法——求出两个字符串的最长公共字符串
问题:有两个字符串str1和str2,求出两个字符串中最长公共字符串. 例如:“acbbsdef”和"abbsced"的最长公共字符串是“bbs” 算法思路: 1.把两个字符串分别 ...
- UML基本关系
UML-Unified Model Language 统一建模语言,又称标准建模语言.是用来对软件密集系统进行可视化建模的一种语言.UML的定义包括UML语义和UML表示法两个元素. UML是在开发阶 ...
- jquery-pjax
项目介绍: Pjax是jQuery的一个插件,Pjax即pushState + Ajax,是实现无刷新Ajax加载并解决浏览器前进和后退问题的一个开源实现. 在2012年8月28日发布0.9版本. P ...
- JQ 获取下一个元素和获取下一个元素的[指定]子元素
<script type="text/javascript"> $(function () { $("#div1").next().addClass ...
- Bootstrap 模态框(Modal)带参数传值实例
模态框(Modal)是覆盖在父窗体上的子窗体.通常,目的是显示来自一个单独的源的内容,可以在不离开父窗体的情况下有一些互动.子窗体可提供信息.交互等. 为了实现父窗体与其的交互,通常需要向其传值,实现 ...
- nginx + php 403 原因分析
环境:nginx + php 问题: 配置的网站,访问出现报错:Access Denied (403) 常见解决方法: 1.文件权限问题 可能是文件权限问题,没有读权限. 或者selinux没有关闭. ...
- style 使用lang = ‘scss’ 报错
<style lang="scss" rel="stylesheet/scss" scoped> .export-wrapper{ } </s ...
- vue向数组中动态添加数据
vue中数据更新通过v-model实现,向数组中添加数据通过push()实现,向shortcuts数组中动态添加newShortcut对象中的title和action this.shortcuts.p ...
- 数据清理,预处理 pandas dataframe 操作技巧 总结
dsoft2 = data1.loc[(data1['程'] == "轻") | (data1['程'] == "中")]设置x下标plt.xticks(np. ...
- 【WPS】表格使用VBA宏编程写入ini文件实现软件多语言
前言:公司软件最近在做多语言版本,而又来一个西班牙文版本的,之前已经做过中文版本,英文版本和法文版本,之前是同事做的,现在安排我做,之前的做法,使用wps表格,翻译好,然后一个一个复制粘贴到ini文件 ...