摘自:http://blog.csdn.net/cstarbl/article/details/7200757
通知栏Notification使用自定义视图方法,这里以显示进度条ProgressBar为例,具体效果不上图了,请参考在Android Market下载软件时通知栏的效果。
布局main.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button android:id="@+id/bt1"
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:text="Notification测试"
- />
- <Button android:id="@+id/bt2"
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:text="清除Notification"
- />
- </LinearLayout>
自定义通知栏的布局:notification.xml文件,一个TextView显示下载中,下面一根进度条显示当前进度
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- >
- <TextView android:id="@+id/down_tv"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:textSize="20sp"
- android:textColor="#000000"
- android:text="下载中"
- />
- <ProgressBar android:id="@+id/pb"
- android:layout_width="260dip"
- android:layout_height="wrap_content"
- style="?android:attr/progressBarStyleHorizontal"
- android:layout_gravity="center_vertical"/>
- </LinearLayout>
程序代码:
- package com.pocketdigi.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.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.RemoteViews;
- public class main extends Activity {
- /** Called when the activity is first created. */
- int notification_id=19172439;
- NotificationManager nm;
- Handler handler=new Handler();
- Notification notification;
- int count=0;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button bt1=(Button)findViewById(R.id.bt1);
- bt1.setOnClickListener(bt1lis);
- Button bt2=(Button)findViewById(R.id.bt2);
- bt2.setOnClickListener(bt2lis);
- //建立notification,前面有学习过,不解释了,不懂看搜索以前的文章
- nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
- notification=new Notification(R.drawable.home,"图标边的文字",System.currentTimeMillis());
- notification.contentView = new RemoteViews(getPackageName(),R.layout.notification);
- //使用notification.xml文件作VIEW
- notification.contentView.setProgressBar(R.id.pb, 100,0, false);
- //设置进度条,最大值 为100,当前值为0,最后一个参数为true时显示条纹
- //(就是在Android Market下载软件,点击下载但还没获取到目标大小时的状态)
- Intent notificationIntent = new Intent(this,main.class);
- PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);
- notification.contentIntent = contentIntent;
- }
- OnClickListener bt1lis=new OnClickListener(){
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- showNotification();//显示notification
- handler.post(run);
- }
- };
- Runnable run=new Runnable(){
- @Override
- public void run() {
- // TODO Auto-generated method stub
- count++;
- notification.contentView.setProgressBar(R.id.pb, 100,count, false);
- //设置当前值为count
- showNotification();//这里是更新notification,就是更新进度条
- if(count<100) handler.postDelayed(run, 200);
- //200毫秒count加1
- }
- };
- OnClickListener bt2lis=new OnClickListener(){
- @Override
- public void onClick(View v) {
- nm.cancel(notification_id);
- //清除notification
- }
- };
- public void showNotification(){
- nm.notify(notification_id, notification);
- }
- }
效果图:
相关推荐
Anroid,一个简单的带按钮自定义通知栏,欢迎下载试用,人人为我,我为人人,共同学习,共同进步
Android 使用Notification自定义通知栏显示。自定义系统状态栏通知类NotificationExtend,也可以手动更新状态栏。依据此项目可以做成线程接受,或者广播接受自动更新。我博客网址: ...
自定义标准视图64dip的通知栏,功能丰富,敬请下载并指点不足。
关于Android 通知栏主要是基于Android 4.x、Android 5.x、Android 7.x为解决界限,例如,你可能解决Android 5.x以上版本标题字体颜色适配问题,却发现通知小图标竟然却是小白块等等。
Android 实现自定义通知栏,并不断更新通知栏的进度条,模拟下载效果。 注:只是模拟下载进度,没有下载功能。
自定义通知栏和个推透传结合 关于个推配置详细见个推官网
自定义通知栏notification,类似酷我音乐在后台运行时,带有按钮的通知栏,可点击下一曲、上一曲、暂停、播放、删除等按钮可响应,可直接运行的源码
微信、淘宝、网易新闻、自定义等通知栏样式,各种通知栏目样式大全
示例一:实现通知栏管理 当针对相同类型的事件多次发出通知,作为开发者,应该避免使用全新的通知,这时就应该考虑更新之前通知栏的一些值来达到提醒用户的目的。例如我们手机的短信系统,当不断有新消息传来时,...
详情:http://blog.csdn.net/onceing/article/details/46911773
本文实例讲述了Android不使用自定义布局情况下实现自定义通知栏图标的方法。分享给大家供大家参考,具体如下: 自定义通知栏图标?不是很简单么。自定义布局都不在话下! 是的,有xml布局文件当然一切都很简单,如果...
自定义通知栏声音 通知栏跳转并传递数据 声音生成可以去 http://download.csdn.net/detail/y1067454341/9554736 下载
5.0之后系统默认通知栏及自定义通知栏以及弹出悬浮自定义通知栏
android自定义布局通知栏,通过通知栏按钮跳转到特定的activity
包括:自定义视图的步骤(声明属性、构造对象、测量尺寸、绘制视图)、自定义简单动画(任务片段、下拉刷新动画、圆弧进度动画)、自定义对话框的操作(对话框、改进日期对话框、自定义多级对话框)、自定义通知栏的...
Android自定义通知显示在通知栏上,小例子
清除自定义通知栏的图标!可以很好的清除拥挤的图标.让你的通知栏更加简明
安卓通知栏自定义布局提示(NotificationCompat.Builder)