Notification学习

做了一个小项目,需要用到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) and setLatestEventInfo(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
方法:设置通知的事件消息

voidsetLatestEventInfo(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:

voidcancel(int id)
Cancel a previously shown notification.    根据通知id取消一个正在显示的通知
voidcancelAll()
Cancel all previously shown notifications. 取消所有 显示的通知(仅是和这个context有关的通知)
voidnotify(int id, Notification notification)    将通知加入状态栏,标记为id
Persistent 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);
}
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值