发送通知

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的更多相关文章

  1. android通知-Notification

    android中,当app需要向发送一些通知,让使用者注意到你想要告知的信息时,可以用Notification.下面,就来讨论一下,Notification的用法,我们从实际的小例子来进行学习. 1. ...

  2. Android 通知(Notification)

    1.介绍 2.常用属性 3.java后台代码 package com.lucky.test30notification; import android.app.Notification; import ...

  3. Android通知Notification全面剖析

    通知 通知是您可以在应用的常规 UI 外部向用户显示的消息.当您告知系统发出通知时,它将先以图标的形式显示在通知区域中.用户可以打开抽屉式通知栏查看通知的详细信息. 通知区域和抽屉式通知栏均是由系统控 ...

  4. Android——通知 Notification

    链接:http://jingyan.baidu.com/article/77b8dc7fde875a6175eab641.html http://www.2cto.com/kf/201502/3749 ...

  5. Android中的通知—Notification 自定义通知

    Android中Notification通知的实现步骤: 1.获取NotificationManager对象NotificationManager的三个公共方法:①cancel(int id) 取消以 ...

  6. Android 主页面顶部栏的通知Notification ,可以自定义通知消息栏的风格,并且点击通知栏进人本程序。

    常用的程序通知,显示到主页面的顶部栏. package com.lixu.tongzhi; import android.app.Activity; import android.app.Notifi ...

  7. Android 状态栏通知Notification、NotificationManager简介

    Notification(通知)一般用在电话,短信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个通知,这时手从上方滑动状态栏就可以展开并处理这个通知: 在Android系统中, ...

  8. Android的状态栏通知(Notification)

    通知用于在状态栏显示消息,消息到来时以图标方式表示,如下: 如果需要查看消息,可以拖动状态栏到屏幕下方即可查看消息. 1.Layout布局文件: <RelativeLayout xmlns:an ...

  9. Android简易实战教程--第三十八话《自定义通知NotifiCation》

    上一篇小案例,完成了一个普通的通知,点击通知启动了一个活动.但是那里的通知没有加入些"靓点",这一篇就给它加入自定义的布局,完成自定义的通知. 应用:比如QQ音乐为例,当点击音乐播 ...

  10. Android开发——Notification通知的各种Style详解

    本来是想与之前讲解使用Notification通知使用一起写的,查看了资料,觉得有必要将这Style部分单独拿出来讲解 前篇:Android开发——Notification通知的使用及Notifica ...

随机推荐

  1. [C#参考]属性

    属性和字段不同,属性是一个函数成员:它提供灵活的机制来读取.编写或计算某个私有字段的值. 可以像使用公共数据成员一样使用属性,但实际上它们是称作“访问器”的特殊方法. 这使得可以轻松访问数据,此外还有 ...

  2. Linux学习之head命令

    head 与 tail 就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块,head 用来显示档案的开头至标准输出中,而 tail 想当然尔就是看档案的结尾. 1.命令格式: hea ...

  3. Fragment和activity之间的通信

    1>fragment可以调用getactivity()方法获取它所在的activity. 2>activity可以调用FragmentManager的findFragmentById()或 ...

  4. JavaScript装逼指南

    如何写JavaScript才能逼格更高呢?怎样才能组织JavaScript才能让别人一眼看出你不简单呢?是否很期待别人在看完你的代码之后感叹一句“原来还可以这样写”呢?下面列出一些在JavaScrip ...

  5. CSS3 Gradient-CSS3渐变

    CSS3 Gradient分为linear-gradient(线性渐变)和radial-gradient(径向渐变).而我们今天主要是针对线性渐变来剖析其具体的用法.为了更好的应用CSS3 Gradi ...

  6. 运用JavaScript构建你的第一个Metro式应用程序(on Windows 8)(一)

    原文 http://blog.csdn.net/zhangxin09/article/details/6784547 作者:Chris Sells 译: sp42   原文 包括 HTML.CSS 和 ...

  7. 分析java中clone()方法 (转载+修改)

    Java中的clone() 方法 java所有的类都是从java.lang.Object类继承而来的,而Object类提供下面的方法对对象进行复制. protected native Object c ...

  8. poj1799---解析几何

    sin(a)=r/R-r,反三角asin(r/R-r),乘以2n=2pi,去化简,得到r 收获:define pi acos(-1) 这样pi的精度会高很多<math.h>(cos,sin ...

  9. Spring构造器注入、set注入和注解注入

    记得刚开始学spring的时候,老师就反复的提到依赖注入和切面,平常的java开发中,在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种方法耦合度太高并且不容易测试,sp ...

  10. A Simple Problem with Integers(线段树,区间更新)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 83822   ...