Notification学习
来源:互联网 发布:火炬之光2mac版mod 编辑:程序博客网 时间:2024/05/21 10:51
做了一个小项目,需要用到notification在通知栏显示通知,学习一下!~~~
构造方法:(来自帮助文档)
Notification()
Constructs a Notification object with everything set to 0.
Notification(Context context, int icon,CharSequence tickerText, long when,CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
已过时。 use
Notification(int,CharSequence,long)
andsetLatestEventInfo(android.content.Context, java.lang.CharSequence, java.lang.CharSequence, android.app.PendingIntent)
.Notification(int icon, CharSequence tickerText, long when)
Constructs a Notification object with the information needed to have a status bar icon without the standard expanded view.
Notification(Parcel parcel)
Unflatten the notification from a parcel方法:设置通知的事件消息
void
setLatestEventInfo(Context context,CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent)
Sets the
contentView
field to be a view with the standard "Latest Event" layout.Notification中的常量字段:(翻译自文档)
DEFAULT_ALL:使用所有默认值,包括声音、震动、闪光灯
DEFAULT_LIGHTS:使用闪光灯
DEFAULT_VIBRATE:使用震动(注意:需要在manifest中添加权限)
DEFAULT_SOUND:使用声音
FLAG_AUTO_CANCEL:当用户点击后这个notification是否应该被取消
FLAG_NO_CLEAR:该通知能被状态栏按钮清除
FLAG_ONGOING_EVENT:在其它事件进行时,这个通知被放置在通知栏,比如打电话时
FLAG_INSISTENT:是否一直进行,直到用户做出响应
contentIntent:设置PendingIntent对象,点击时发送该Intent
defaults:添加默认效果
flags:设置flag标记位
icon:设置图标
sound:设置声音
tickerText:显示在状态栏中的文字
when:发送此通知的时间印记
NotificationManager:
void
cancel(int id)
Cancel a previously shown notification. 根据通知id取消一个正在显示的通知
void
cancelAll()
Cancel all previously shown notifications. 取消所有 显示的通知(仅是和这个context有关的通知)
void
notify(int id, Notification notification)
将通知加入状态栏,标记为idPersistent notification on the status bar,
Notification获取需要这样:NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
代码:
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/clear"
android:text="取消通知"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/add"
android:text="添加通知"
/>
</LinearLayout>
Activity中代码:
package com.example.notification;
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button clear = null;
private Button add = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
clear = (Button) findViewById(R.id.clear);
add = (Button) findViewById(R.id.add);
clear.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
doClearNotification();
}
});
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
doAddNotification();
}
});
}
public void doClearNotification() {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(0);
}
public void doAddNotification() {
//创建一个NotificationManager对象
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.icon;
CharSequence tickerTest = "Hello";
long when = System.currentTimeMillis();
//创建一个Notification对象
Notification notification = new Notification(icon,tickerTest,when);
//设置Notification的flag属性
notification.flags |= Notification.FLAG_NO_CLEAR;
notification.flags |=Notification.FLAG_ONGOING_EVENT;
notification.flags |=Notification.FLAG_SHOW_LIGHTS;
notification.defaults |= Notification.DEFAULT_ALL;
//设置通知的事件消息
CharSequence contentTitle = "Hello Android";
CharSequence contentText = "Welcome to Android World";
Intent notifyIntent = new Intent(MainActivity.this,MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notifyIntent, 0);
notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);
//将notification传递给manager
manager.notify(0,notification);
}
}
- notification学习
- Notification学习
- Notification学习
- Notification 学习
- Android Notification学习--发出Notification
- Linux Notification 学习
- Notification学习(转载)
- Android学习之Notification
- Linux Notification chain学习
- iOS学习--notification
- Notification 学习笔记
- Linux Notification 学习
- 学习status bar notification
- android的Notification学习
- Android Services学习--Notification
- Notification类的学习
- Android学习之 Notification
- Ceilometer Notification代码学习
- JDBC连接池技术
- Android使用Fragment来实现ViewPager的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信
- 学习共享,集思广益。
- 遍历指定文件夹中的Excel
- java关机工具之页面布局参考代码
- Notification学习
- 为什么Java7开始在数字中使用下划线(Java7怎样在数字中使用下划线)
- crsd.log: th_select_handler message repeated [ID 338693.1]
- android settings 导入到eclipse中并安装Settings.apk2014.9.20
- 信管1132-30第一张绪论知识导图
- Service 生命周期和使用注意项
- leetcode - Linked List Cycle
- 在屏幕中央绘制一个圆点
- 推荐系统