- 浏览: 6310572 次
- 性别:
- 来自: 一片神奇的土地
文章分类
- 全部博客 (745)
- JQuery (25)
- JS (33)
- 数据库 (59)
- Java基础 (56)
- JSON (8)
- XML (8)
- ireport (7)
- 设计模式 (10)
- 心情 (14)
- freemarker (1)
- 问题 (15)
- powerdesigner (2)
- CSS (15)
- DWR (4)
- tomcat (16)
- Hibernate (12)
- Oracle (7)
- Struts (7)
- Spring (34)
- JSP (23)
- 需学习 (64)
- 工具类库 (63)
- Maven (14)
- 笔试题 (34)
- 源码学习 (31)
- 多线程 (39)
- Android (32)
- 缓存 (20)
- SpringMVC (14)
- jQueryEasyUi (12)
- webservice-RPC (13)
- ant (1)
- ASP.NET (10)
- 正则表达式 (3)
- Linux (15)
- JBoss (1)
- EJB (3)
- UML (2)
- JMS (3)
- Flex (8)
- JSTL (2)
- 批处理 (5)
- JVM (16)
- 【工具】 (16)
- 数据结构 (29)
- HTTP/TCP/Socket (18)
- 微信 (1)
- tomcat源码学习 (15)
- Python (30)
- 主机 (2)
- 设计与架构 (19)
- thrift-RPC (2)
- nginx (6)
- 微信小程序 (0)
- 分布式+集群 (12)
- IO (1)
- 消息队列 (4)
- 存储过程 (8)
- redis (9)
- zookeeper (5)
- 海量数据 (5)
最新评论
-
360pluse:
技术更新,战术升级!Python爬虫案例实战从零开始一站通网盘 ...
Python爬虫实战:Scrapy豆瓣电影爬取 -
18335864773:
推荐用 pageoffice 组件生成 word 文件。
JAVA生成WORD工具类 -
jjhe369:
LISTD_ONE 写道起始地址为163.135.0.1 结束 ...
IP地址与CIDR -
baojunhu99:
private final int POOL_SIZE = 5 ...
使用CompletionService获取多线程返回值 -
LovingBaby:
胡说,javascript 运行时是单线程的,event lo ...
Ajax请求是否可以实现同步
1、获得系统服务
notificationmanager=(NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
2、创建Notification
notification=new Notification(R.drawable.keai,"可爱",System.currentTimeMillis());
3、对Notification进行一些设置
notification.flags=Notification.FLAG_AUTO_CANCEL; //设置下拉点击之后回到应用程序,可以有多个值选择
4、Intent与PendingIntent
Intent intent=new Intent(this,Z18Activity.class);
pendingintent=PendingIntent.getActivity(this,0,intent,0);
notification.setLatestEventInfo(this,"真可爱","太可爱啦!",pendingintent);
5、可更改提示下拉条中的布局
RemoteViews rv=new RemoteViews(this.getPackageName(),R.layout.main); //此步骤是修改下拉后看到提示条的布局
notification.contentView=rv;
6、用NotificationManager发送Notification提示信息
notificationmanager.notify(0,notification); //通过标识发送指定的Notification
上面有些步骤是属于并列的,可能后面的操作将覆盖前面的操作
Example1:
Ticker Text:Notification刚出来的时候,在状态栏上滚动的字幕,如果很长,会自动分割滚动
Content Title:Notification展开后的标题
Content Text:Notification展开后的内容
取得NotificationManage
private NotificationManager mNotificationManager; mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
创建Notification并且显示
//Notification的滚动提示 String tickerText = "My notification, It's a long text! Hello World desiyo?"; //Notification的图标,一般不要用彩色的 int icon = R.drawable.icon_02241_3; //contentTitle和contentText都是标准的Notification View的内容 //Notification的内容标题,拖下来后看到的标题 String contentTitle="My notification"; //Notification的内容 String contentText="Hello World!"; //Notification的Intent,即点击后转向的Activity Intent notificationIntent = new Intent(this, this.getClass()); notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); //创建Notifcation Notification notification = new Notification(icon, tickerText, System.currentTimeMillis()); //设定Notification出现时的声音,一般不建议自定义 notification.defaults |= Notification.DEFAULT_SOUND; //设定如何振动 notification.defaults |= Notification.DEFAULT_VIBRATE; //指定Flag,Notification.FLAG_AUTO_CANCEL意指点击这个Notification后,立刻取消自身 //这符合一般的Notification的运作规范 notification.flags|=Notification.FLAG_AUTO_CANCEL; notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent); //显示这个notification mNotificationManager.notify(HELLO_ID, notification);
这是最基本的应用,可以说除了找个合适的图标以外,其它都很简单。
使用自定义View的Notification
首先给出View的定义文件:notification_view_sample.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="3dp" > <ImageView android:id="@+id/notificationImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@android:drawable/stat_sys_download" /> <TextView android:id="@+id/notificationTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/notificationImage" android:layout_alignParentRight="true" android:paddingLeft="6dp" android:textColor="#FF000000" /> <TextView android:id="@+id/notificationPercent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/notificationImage" android:paddingTop="2dp" android:textColor="#FF000000" /> <ProgressBar android:id="@+id/notificationProgress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/notificationTitle" android:layout_alignLeft="@id/notificationTitle" android:layout_alignParentRight="true" android:layout_alignTop="@id/notificationPercent" android:paddingLeft="6dp" android:paddingRight="3dp" android:paddingTop="2dp" style="?android:attr/progressBarStyleHorizontal" /> </RelativeLayout>
//Notification的滚动提示 String tickerText1 = "Custom view for download notification"; //Notification的图标,一般不要用彩色的 int icon1 = android.R.drawable.stat_sys_download; //Notification的Intent,即点击后转向的Activity Intent notificationIntent1 = new Intent(this, this.getClass()); notificationIntent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent contentIntent1 = PendingIntent.getActivity(this, 0, notificationIntent1, 0); //创建Notifcation Notification notification1 = new Notification(icon1, tickerText1, System.currentTimeMillis()); //设定Notification出现时的声音,一般不建议自定义 notification1.defaults |= Notification.DEFAULT_SOUND; //设定是否振动 notification1.defaults |= Notification.DEFAULT_VIBRATE; //notification.number=numbers++; //指定Flag,Notification.FLAG_AUTO_CANCEL意指点击这个Notification后,立刻取消自身 //这符合一般的Notification的运作规范 notification1.flags|=Notification.FLAG_ONGOING_EVENT; //创建RemoteViews用在Notification中 RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_view_sample); contentView.setTextViewText(R.id.notificationTitle, "Download:Facebook for android"); contentView.setTextViewText(R.id.notificationPercent, "35%"); contentView.setProgressBar(R.id.notificationProgress, 100, 35, false); notification1.contentView = contentView; notification1.contentIntent=contentIntent1; //显示这个notification mNotificationManager.notify(CUSTOM_VIEW_ID, notification1);
注意以上代码中使用的是RemoteViews,而不是普通的View,另外使用的是PendingIntent而不是普通的Intent,这都说明了 Notification是1个“远程”的东西,其中能够使用的控件是受限制的,比如说TableLayout就不能使用。看下效果图,是不是和 Market中的界面很接近呢?
由于在使用自定义的view时不必使用setLastEventInfo()方法,所以你必须为Notification的contentIntent域定义一个Intent,如下所示:
RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.notification_view); contentView.setTextViewText(R.id.notificationTitle, "下载文件:测试文件"); contentView.setTextViewText(R.id.notificationPercent, "55%"); contentView.setProgressBar(R.id.notificationProgress, 100, 55, false); notice.contentView = contentView; notice.contentIntent = contentIntent;
注意:当为notification创建自定义的布局时,你必须确保它能够再不同的设备方向以及分辨率下都正常工作。这对于所有的布局工作都是十分重要的,所以,不要定义过于复杂的布局,而且要在多种情况下进行测试。
更好的控制Notification
动画图标怎么做?
和selector类似,定义1个XML文件放在drawable下,下面是之前用到的stat_sys_download的定义:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/stat_sys_download_anim0" android:duration="200" /> <item android:drawable="@drawable/stat_sys_download_anim1" android:duration="200" /> <item android:drawable="@drawable/stat_sys_download_anim2" android:duration="200" /> <item android:drawable="@drawable/stat_sys_download_anim3" android:duration="200" /> <item android:drawable="@drawable/stat_sys_download_anim4" android:duration="200" /> <item android:drawable="@drawable/stat_sys_download_anim5" android:duration="200" /> </animation-list>
如何更新Notification?
注意到前面的代码中用到的CUSTOM_VIEW_ID,这是Notification的ID,如果2次弹出的Notification的ID相同,那么Notification就只会更新而不会再次滚动提醒。之前给出的ProgressBar是不会动的,利用这个方法就可以让它动起来(或者也可以直接调用RemoteView的set方法来直接更新?未试验)
如何自定义提示的声音和振动
?
//自定义提示音notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
//自定义振动方式long[] vibrate = {0,100,200,300};notification.vibrate = vibrate;
请注意:如果使用了DEFAULT_SOUND或DEFAULT_VIBRATE,则自定义的提示音和振动无效。
在类似于短消息的应用中如何提示数量?
使用Notification的number属性,默认为0,如果是1或更大的数字,则会在图标上覆盖显示这个数字。
notification.number=notificationNumber;
Flag的使用
notification有1个flag属性,除了DEFAULT_SOUND之外,还有几个很有用的属性。
FLAG_AUTO_CANCEL:自动清除Notification,前面的例子中有用到
FLAG_INSISTENT:提示音一直不停,直至用户响应(很吵吧!)
FLAG_ONGOING_EVENT:表示这是1个正在进行的任务,不可以清除,第2个例子中有用到
FLAG_NO_CLEAR:不可以清除
实例来源:
http://www.apkbus.com/forum.php?mod=viewthread&tid=20486
参考:http://www.apkbus.com/forum.php?mod=viewthread&tid=19156
Example2:
Button btn4 = (Button) this.findViewById(R.id.btn4); btn4.setText("发出一个通知(Notification)"); btn4.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { // 实例化通知管理器 NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // 指定单击通知后所打开的详细的通知页面(单击通知后打开 NotificationView) PendingIntent contentIntent = PendingIntent.getActivity( Main.this, 0, new Intent(Main.this, NotificationView.class), 0); // 实例化一个通知,并指定其图标和标题(在提示栏上显示) Notification n = new Notification(R.drawable.icon01, "我是滚动的通知信息我是滚动的通知信息我是滚动的通知信息", System.currentTimeMillis()); // 设置通知的发送人和通知的详细内容(打开提示栏后在通知列表中显示) n.setLatestEventInfo(Main.this, "通知发送人", "我是详细的通知信息我是详细的通知信息我是详细的通知信息", contentIntent); // 100 毫秒延迟后,震动 250 毫秒,暂停 100 毫秒后,再震动 500 毫秒 n.vibrate = new long[] { 100, 250, 100, 500 }; // 发出通知(其中第一个参数为通知标识符) nm.notify(0, n); } });
// 单击通知列表的某个通知后,所打开的详细的通知页 public class NotificationView extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.view); TextView txtMsg = (TextView)this.findViewById(R.id.txtMsg); txtMsg.setText("点通知之后所链接到的 Activity"); NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // 取消显示在通知列表中的指定通知(参数为通知标识符) nm.cancel(0); // 需要关闭此 Activity 的话就 finish 它既可 // this.finish(); } }
...
Example3:
public class NotificationActivity extends Activity implements OnClickListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.notification); findViewById(R.id.notificationBtn).setOnClickListener(this); } @Override public void onClick(View arg0) { // TODO Auto-generated method stub addNotification(); } private void addNotification() { NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(); notification.icon = R.drawable.icon; notification.tickerText = "我在这里"; notification.defaults = Notification.DEFAULT_SOUND; notification.audioStreamType = android.media.AudioManager.ADJUST_LOWER; Intent intent = new Intent(this, Notification2Activity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); notification.setLatestEventInfo(this, "短信通知", "亲爱的,晚上8点老地方见哦~", pendingIntent); manager.notify(R.drawable.icon, notification); } }
public class Notification2Activity extends Activity implements OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.notification2); findViewById(R.id.cancleBtn).setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub cancleNotification(); } private void cancleNotification() { // TODO Auto-generated method stub NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); manager.cancel(R.drawable.icon); Toast.makeText(this, "Notification cancled", Toast.LENGTH_SHORT).show(); } }
都是很简单的例子,帮助理解!
发表评论
-
选择照片和拍照
2018-06-13 15:31 1629Android拍照或从本地选择图片上传 Android ... -
PullToRefresh的简单使用
2018-05-31 16:06 1252Android下拉刷新控件--PullToRefresh的简 ... -
fragment内嵌fragment之间传值+切换fragment
2018-05-30 11:59 13382跳转: 从一个Fragment跳转到另一个Fragmen ... -
控件使用问题
2018-05-22 09:59 1502TextView最大长度限制,超出部分省略号显示 xml ... -
ViewPager+RadioGroup+TabLayout
2018-05-22 09:38 1076ViewPager: ViewPager 详解(五)-- ... -
网络请求工具类HttpUtils
2018-05-19 21:07 2387代码如下: import android.app.Acti ... -
android排版布局学习
2018-05-18 16:24 3832Android开发学习之路--UI之基本布局 andro ... -
[工具]-Android切图
2018-05-18 14:21 1471切图小科普!聊聊原生APP切图那些事儿 安卓APP设计规 ... -
【Android基础问题】
2018-05-18 13:49 7021、appcompat_v7: appcompat_v7 ... -
WebView使用
2018-05-17 17:42 1153Android:最全面的 Webvie ... -
《第一行代码》服务+AsyncTask+定时任务
2018-04-23 09:45 173814、服务 服务(Service)是 Androi ... -
《第一行代码》通知+接收拦截发送短信
2018-04-23 09:44 166113、通知 在活动里 ... -
《第一行代码》内容提供器
2018-04-20 16:08 123012、内容提供器 内容提供器(Content Pro ... -
《第一行代码》数据存储
2018-04-20 16:07 63411、数据存储 将数据存储到文件中 ope ... -
《第一行代码》广播
2018-04-20 15:34 95410、广播接收器(Broadcast Receive ... -
《第一行代码》控件、布局、碎片
2018-04-20 14:40 8788、控件 TextView: a ... -
【Android studio配置和错误】
2018-04-20 14:21 3937Gradle是一个基于Apache Ant和Apache ... -
《第一行代码》基础总结
2016-07-12 22:07 1868activity的生命周期—— ... -
《第一行代码》扩展总结
2016-06-29 15:28 1301Android更新UI的两种方法——handler与run ... -
Android快速开发工具类
2016-01-21 14:56 1956来源:http://blog.csdn.net/lmj62 ...
相关推荐
本文实例讲解了通知Notification使用方法,此知识点就是用作通知的显示,包括振动、灯光、声音等效果,分享给大家供大家参考,具体内容如下 效果图: MainActivity: import java.io.File; import android....
Android实现Notification的通知栏常驻.zip
Android 通知(notification)简单实用Demo,包含点击功能简单例子,供大家下载学习
主要为大家详细介绍了Android中通知Notification的使用方法,感兴趣的小伙伴们可以参考一下
这几天做一个小软件在API28(Android 9.0)的模拟器上测试时,发现通知栏无效,经过一番查询,了解到:API26(Android 8.0)以后,引入了**通知渠道(Notification Channels)**这么一个东西来帮助用户管理通知。...
Android中使用Notification在通知栏中显示通知示例代码.rar Android中使用Notification在通知栏中显示通知示例代码.rar Android中使用Notification在通知栏中显示通知示例代码.rar
android消息通知栏Notification
这个是android notification框架,如有需要请下载。发发发
notification是一种出现在任务栏的提示,特别是在4.0以后notification改进了不少,本文内容都是基于4.0及4.1以后总结来的,详细介绍各种布局的Notification样式,实现音乐播放器、邮件通知等复杂的例子详细代码。
修改android 2.3版本Notification通知栏不显示的Bug Android 服务端下载 Notification通知栏 断点下载
发送通知在ActionBar上显示,Notification
使用android push notification service 实现即时通知
主要利用了notification service 和receiver 实现了用户通知栏更新,失败了点击重新下载,下载好了点击自动安装,布局很丑,请根据自己项目的实际需求自行修改
示例一:实现通知栏管理 当针对相同类型的事件多次发出通知,作为开发者,应该避免使用全新的...为了更新你之前发布的通知,你需要更新或者创建一个NotificationCompat.Builder对象,从之前的通知中创建一个Notifica
这个是通知栏框架(Notificaiton)的全面学习,里面把大概所有的情况都列了出来,通过一个DEMO让你了解它的大致所有使用过程。 可以通过以下博文进行配套了解(有效果图): ...
AlarmManager实质是一个全局的定时器,是Android中常用的一种系统级别的提示服务,在指定时间或周期性启动其它组件(包括Activity,Service,BroadcastReceiver)。本文将讲解一下如何使用AlarmManager实现定时提醒...
Android的各种通知Notification、Dialog、Toast、Snackbar
NULL 博文链接:https://sunzone.iteye.com/blog/1998123
Android Notification通知 简单使用 demo下载。 解决问题:兼容高低版本,帮助用户打开通知开关,android5.x手机状态栏icon是白块问题。 详细文章请参考 ...