android-通知Notification
发送通知
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
String serviceContextName = Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager;
notificationManager = (NotificationManager) getSystemService(serviceContextName);
//设置图标,标题,时间
int icon = R.drawable.ic_launcher;
String tickerText = "Notification";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
notification.number++;
//设置展开通知的内容
Context context = getApplicationContext();
String expandedText = "Extended status text";
String expandedTitle = "Notification Title";
//当单击展开的文本时,用于启动一个活动的意图
Intent intent = new Intent(this, MyActivity.class);
PendingIntent launchIntent = PendingIntent.getActivity(context, 0, intent, 0);
notification.setLatestEventInfo(context, expandedText, expandedText, launchIntent);
//若要定制布局(若手动设置contentView时,必须同时设置contentIntent)
notification.contentView = new RemoteViews(this.getPackageName(), R.layout.my_status_window_layout);
notification.contentIntent = launchIntent;
//修改通知中的视图
notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.ic_launcher);//将后者搬到前者这个id的视图上
notification.contentView.setTextViewText(R.id.status_text, "Current Progress");
notification.contentView.setProgressBar(R.id.status_progress, 100, 50, false);
//触发通知
int notificationRef = 1;
notificationManager.notify(notificationRef, notification);
//消除通知
notificationManager.cancel(notificationRef);
notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE; //设置上默认的声音和震动
//自定义通知声音
Uri ringURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notification.sound = ringURI;
//自定义通知震动,需添加一个permission <uses-permission android:name="android.permission.VIBRATE" />
long[] vibrate = new long[] {1000,1000,1000,1000,1000};//一个值震动多久,下一个值暂定多久,这里持续了5秒
notification.vibrate = vibrate;
//自定义通知闪屏
notification.ledARGB = Color.RED;
notification.ledOffMS = 0;//设置频率
notification.ledOnMS = 1; //若OnMS也是0,则将灯关闭
notification.flags = notification.flags | Notification.FLAG_SHOW_LIGHTS;
//持续的通知
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;//正在进行的事件
//连续的通知
notification.flags = notification.flags | Notification.FLAG_INSISTENT;//一直重复音频震动闪烁直到被取消
}
}
警报
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//警报。警报类型:
// RTC_WAKEUP指定时间唤醒并触发意图;
// RTC指定时间触发意图但不唤醒设备;
// ELAPSED_REALTIME设备启动后经过的时间触发待处理的意图,但不唤醒设备;
// ELAPSED_REALTIME_WAKEUP设备启动并经过指定时间后唤醒设备并触发意图
AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP;
long timeOrLengthofWait = 10000;
String ALARM_ACTION = "ALARM_ACTION";
Intent intentToFire = new Intent(ALARM_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intentToFire, 0);
alarms.set(alarmType, timeOrLengthofWait, pendingIntent);
//取消报警
alarms.cancel(pendingIntent);
//重复报警
//如果已经唤醒,那么每小时触发一次意图
alarms.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 60*60*1000, 60*60*1000, pendingIntent);
//每小时唤醒并触发一个警报
alarms.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 60*60*1000, AlarmManager.INTERVAL_DAY, pendingIntent);
}
}
android-通知Notification的更多相关文章
- android通知-Notification
android中,当app需要向发送一些通知,让使用者注意到你想要告知的信息时,可以用Notification.下面,就来讨论一下,Notification的用法,我们从实际的小例子来进行学习. 1. ...
- Android 通知(Notification)
1.介绍 2.常用属性 3.java后台代码 package com.lucky.test30notification; import android.app.Notification; import ...
- Android通知Notification全面剖析
通知 通知是您可以在应用的常规 UI 外部向用户显示的消息.当您告知系统发出通知时,它将先以图标的形式显示在通知区域中.用户可以打开抽屉式通知栏查看通知的详细信息. 通知区域和抽屉式通知栏均是由系统控 ...
- Android——通知 Notification
链接:http://jingyan.baidu.com/article/77b8dc7fde875a6175eab641.html http://www.2cto.com/kf/201502/3749 ...
- Android中的通知—Notification 自定义通知
Android中Notification通知的实现步骤: 1.获取NotificationManager对象NotificationManager的三个公共方法:①cancel(int id) 取消以 ...
- Android 主页面顶部栏的通知Notification ,可以自定义通知消息栏的风格,并且点击通知栏进人本程序。
常用的程序通知,显示到主页面的顶部栏. package com.lixu.tongzhi; import android.app.Activity; import android.app.Notifi ...
- Android 状态栏通知Notification、NotificationManager简介
Notification(通知)一般用在电话,短信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个通知,这时手从上方滑动状态栏就可以展开并处理这个通知: 在Android系统中, ...
- Android的状态栏通知(Notification)
通知用于在状态栏显示消息,消息到来时以图标方式表示,如下: 如果需要查看消息,可以拖动状态栏到屏幕下方即可查看消息. 1.Layout布局文件: <RelativeLayout xmlns:an ...
- Android简易实战教程--第三十八话《自定义通知NotifiCation》
上一篇小案例,完成了一个普通的通知,点击通知启动了一个活动.但是那里的通知没有加入些"靓点",这一篇就给它加入自定义的布局,完成自定义的通知. 应用:比如QQ音乐为例,当点击音乐播 ...
- Android开发——Notification通知的各种Style详解
本来是想与之前讲解使用Notification通知使用一起写的,查看了资料,觉得有必要将这Style部分单独拿出来讲解 前篇:Android开发——Notification通知的使用及Notifica ...
随机推荐
- java.lang.ArithmeticException: / by zero
- @synthesize 与@dynamic区别
@synthesize 除非开发人员已经做了,否则由编译器自动生成getter/setter方法. 当开发人员自定义存或取方法时,自定义会屏蔽自动生成该方法. @dynamic 告诉编译器,不自动生成 ...
- ES6笔记③
1.查找关键字 includes(); 返回布尔值 //①:includes -->代替-->indexof-->返回布尔值 var str = "769909303&q ...
- 加密传输SSL协议7_SSL协议概述
SSL(Secure Sockets Layer) SSL的功能,可以在通信的双方中建立一个加密的通信通道 同时还可以确认通信的双方是不是就是其声称的人,防止被钓鱼. SSL在网络协议栈中的位置:可以 ...
- mysql 5.6.20的安装、配置服务、设置编码格式
一.安装 安装环境 系统:Window 32 版本:Mysql 5.6.20 1. 首先从官网上http://dev.mysql.com/downloads/mysql/ ...
- mysql 与 mysqli的区别
mysqli.dll是PHP对mysql新特性的一个扩展支持.在PHP5中可以在php.ini中加载mysql后面的i,指improved, interface, ingenious, incompa ...
- 正确的注销PHP SESSION
/* 1.每个页面都必须开启session_start()后才能在每个页面里面使用session. 2.session_start()初始化session,第一次访问会生成一个唯一会话ID保存在客户端 ...
- linux----定义命令别名
1.定义命令别名的语法: alias nickName='command'#用于定义. unalias nickName#用于撤消一个别名的定义. 如:alias cls='clear' 2.应该要 ...
- 信号和槽有一个非常精炼的C++实现,作者是Sarah Thompson,该实现只有一个头文件sigslot.h,跨平台且线程安全
关于信号和槽有一个非常精炼的C++实现,作者是Sarah Thompson,该实现只有一个头文件sigslot.h,跨平台且线程安全. 源码在:http://sigslot.cvs.sourcefor ...
- Qt中如何禁掉所有UI操作以及注意事项(处理各个widget的eventFilter这一层,但是感觉不好,为什么不使用QApplication呢)
刚做完的一个项目,在测试时出现了一个问题:由于多线程的存在,当进行语音识别时:如果用户点击程序界面上的button或者其他接受点击事件后会发出信号的widget时,程序会crash ! 后来尝试着从多 ...