Notification的简单用法

 Notification 代表的是一种全局效果的通知,一般通过NotificationManager服务来发送Notification.

下面简单介绍一个例子   例子是我从疯狂Android讲义里面找到的

public class NotificationTest extends Activity
{
static final int NOTIFICATION_ID = 0x123;
NotificationManager nm;


@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获取系统的NotificationManager服务
nm = (NotificationManager) 
getSystemService(NOTIFICATION_SERVICE);
}


// 为发送通知的按钮的点击事件定义事件处理方法
public void send(View source)
{
// 创建一个启动其他Activity的Intent
Intent intent = new Intent(NotificationTest.this
, OtherActivity.class);
PendingIntent pi = PendingIntent.getActivity(
NotificationTest.this, 0, intent, 0);
Notification notify = new Notification.Builder(this)
// 设置打开该通知,该通知自动消失
.setAutoCancel(true)
// 设置显示在状态栏的通知提示信息
.setTicker("有新消息")
// 设置通知的图标
.setSmallIcon(R.drawable.notify)
// 设置通知内容的标题
.setContentTitle("一条新通知")
// 设置通知内容
.setContentText("恭喜你,您加薪了,工资增加20%!")
// // 设置使用系统默认的声音、默认LED灯
// .setDefaults(Notification.DEFAULT_SOUND
// |Notification.DEFAULT_LIGHTS)
// 设置通知的自定义声音
.setSound(Uri.parse("android.resource://org.crazyit.ui/"
+ R.raw.msg))
.setWhen(System.currentTimeMillis())
// 设改通知将要启动程序的Intent
.setContentIntent(pi).build();
// 发送通知
nm.notify(NOTIFICATION_ID, notify);
}


// 为删除通知的按钮的点击事件定义事件处理方法
public void del(View v)
{
// 取消通知
nm.cancel(NOTIFICATION_ID);
}
}

当点击Notification 时我们就会跳转到另一个自己定义的Activity   

源代码public class NotificationTest extends Activity
{
static final int NOTIFICATION_ID = 0x123;
NotificationManager nm;


@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获取系统的NotificationManager服务
nm = (NotificationManager) 
getSystemService(NOTIFICATION_SERVICE);
}


// 为发送通知的按钮的点击事件定义事件处理方法
public void send(View source)
{
// 创建一个启动其他Activity的Intent
Intent intent = new Intent(NotificationTest.this
, OtherActivity.class);
PendingIntent pi = PendingIntent.getActivity(
NotificationTest.this, 0, intent, 0);
Notification notify = new Notification.Builder(this)
// 设置打开该通知,该通知自动消失
.setAutoCancel(true)
// 设置显示在状态栏的通知提示信息
.setTicker("有新消息")
// 设置通知的图标
.setSmallIcon(R.drawable.notify)
// 设置通知内容的标题
.setContentTitle("一条新通知")
// 设置通知内容
.setContentText("恭喜你,您加薪了,工资增加20%!")
// // 设置使用系统默认的声音、默认LED灯
// .setDefaults(Notification.DEFAULT_SOUND
// |Notification.DEFAULT_LIGHTS)
// 设置通知的自定义声音
.setSound(Uri.parse("android.resource://org.crazyit.ui/"
+ R.raw.msg))
.setWhen(System.currentTimeMillis())
// 设改通知将要启动程序的Intent
.setContentIntent(pi).build();
// 发送通知
nm.notify(NOTIFICATION_ID, notify);
}


// 为删除通知的按钮的点击事件定义事件处理方法
public void del(View v)
{
// 取消通知
nm.cancel(NOTIFICATION_ID);
}
源代码http://download.csdn.net/detail/kluing/7398389

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值