Android实现为Notification加上一个进度条的方法
作者:老码农豆豆 发布时间:2022-12-14 00:39:01
标签:Android,Notification,进度条
本文实例讲述了Android实现为Notification加上一个进度条的方法。分享给大家供大家参考,具体如下:
package com.notification;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;
import android.widget.Toast;
public class nofificationActivity extends Activity implements OnClickListener {
private static final int NOTIFICATION_ID = 0x12;
private Notification notification = null;
private NotificationManager manager = null;
public Handler handler;
private int _progress = 0;
private Thread thread = null;
private boolean isStop = false;
// 当界面处理停止的状态 时,设置让进度条取消
@Override
protected void onPause() {
// TODO Auto-generated method stub
isStop = false;
manager.cancel(NOTIFICATION_ID);
super.onPause();
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.Button01);
btn.setOnClickListener(this);
notification = new Notification(R.drawable.icon, "带进条的提醒", System
.currentTimeMillis());
notification.icon = R.drawable.icon;
// 通过RemoteViews 设置notification中View 的属性
notification.contentView = new RemoteViews(getApplication()
.getPackageName(), R.layout.custom_dialog);
notification.contentView.setProgressBar(R.id.pb, 100, 0, false);
notification.contentView.setTextViewText(R.id.tv, "进度" + _progress
+ "%");
// 通过PendingIntetn
// 设置要跳往的Activity,这里也可以设置发送一个服务或者广播,
// 不过在这里的操作都必须是用户点击notification之后才触发的
notification.contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, remoteView.class), 0);
// 获得一个NotificationManger 对象,此对象可以对notification做统一管理,只需要知道ID
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
isStop = true;
manager.notify(NOTIFICATION_ID, notification);
thread = new Thread(new Runnable() {
@Override
public void run() {
Thread.currentThread();
// TODO Auto-generated method stub
while (isStop) {
_progress += 10;
Message msg = handler.obtainMessage();
msg.arg1 = _progress;
msg.sendToTarget();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
thread.start();
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
notification.contentView.setProgressBar(R.id.pb, 100, msg.arg1,
false);
notification.contentView.setTextViewText(R.id.tv, "进度"
+ msg.arg1 + "%");
manager.notify(NOTIFICATION_ID, notification);
if (msg.arg1 == 100) {
_progress = 0;
manager.cancel(NOTIFICATION_ID);
isStop = false;
Toast.makeText(nofificationActivity.this, "下载完毕", 1000)
.show();
}
super.handleMessage(msg);
}
};
}
}
希望本文所述对大家Android程序设计有所帮助。
0
投稿
猜你喜欢
- 为什么要用Flyway在日常开发中,我们经常会遇到下面的问题:自己写的SQL忘了在所有环境执行;别人写的SQL我们不能确定是否都在所有环境执
- 本文实例讲述了C#实现的xml操作类,分享给大家供大家参考,具体如下:using System;using System.Data;usin
- IntroC# 9 中进一步增强了模式匹配的用法,使得模式匹配更为强大,我们一起来了解一下吧SampleC# 9 中增强了模式匹配的用法,增
- 好久没有写过博客了,最近因项目需求,需要用到Socket来进行通信,简单写了几个例子,记录一下,代码很简单,无非就是接收与发送,以及接收到数
- 目录如何快速获取 相册分类一些异常情况的处理Recycleview-CursorAdapter还有必要用LoaderManager吗总结如何
- 摘要:本文介绍Linq查询基本操作(查询关键字)- from 子句- where 子句- select子句- group 子句- into
- 随着JDK 14的发布(https://waylau.com/jdk-14-released/),各大Java IDE也开始支持JDK 14
- 在看KMP算法时,想要简单的统计一下执行时间和性能。得出的结论是: Java的String的indexOf方法性能最好,其次是KMP算法,其
- 步骤:1、遍历D盘Source文件夹找出所有名称包含LTE的文件,文件路径存放到List<string>中2、遍历List<
- 指定创建派生类实例时应调用的基类构造函数;调用基类上已被其他方法重写的方法。注意:不能从静态方法中使用base关键字,base关键字只能在实
- 目录前言一、Spring Boot对Redis的支持二、实战1、添加依赖2、redis配置3、实现序列化4、创建Redis连接工厂,同时注册
- 下面就WPF项目框架搭建步骤一步一步的分享给大家。在WPF项目开发中最常用的开发模式无疑是MVVM模式, MVVM模式开发的好处
- 前言日常开发中,特别是音视频开发,需要在界面上渲染视频,比如制作一个播放器、或者视频编辑工具、以及视频会议客户端。通常拿到的是像素格式数据,
- 1. AIE (演示地址)AIE是一个开源的ajax图片编辑器,基于ExtJS与PHP ImageMagick开发,易于与博客/相册等其它应
- **前言:**我们项目中可能有这种需求,每个人请求了哪些接口?做了什么事情?参数是什么?重要的接口我们需要记录操作日志以便查找。操作日志和系
- 一. 方法重写在面向对象中,实现多态的必备条件是继承、重写和向上转型,现在我们已经学习了什么是继承。接下来我们再来学习什么是方法重写,这是我
- 一、题目描述题目实现:运行客户端,连接服务器。二、解题思路首先需要启动上题的ServerSocketFrame服务,这样客户端运行时,才能连
- 要判断输入金额为正确金额的方法有两个,一个是用正则表达式,另一个就是用textfield的代理方法有时候难免遇到这样的需求,不符合规则的金额
- 无论使用何种IDE开发Android,集成官方Android SDK并创建Android工程之后,该工程都会默认包括一整套Android项目
- 静态方法可以不用创建对象就调用,非静态方法必须有了对象的实例才能调用。因此想在静态方法中直接引用非静态方法是不可能的,因为不知道调用哪个对象