notification

在Android开发过程中可能会遇到一些需要消息通知提醒之类的功能,Notificatino 和 NotificationManager 搭配使用可以很好的完成工作。

这里有个小例子,在MainActivity中启动一个Notification,在ClearNotification中将其取消。

MainActivity.java

package com.win.notificationdemo;

import android.os.Build;
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.widget.Button;

public class MainActivity extends Activity {

private Button mBtn;
public static final int NOTIFICATION_ID = 101;
private NotificationManager nm;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

mBtn = (Button) findViewById(R.id.demo_btn);
mBtn.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

// Notification n = new Notification(R.drawable.ic_launcher, getResources().getText(R.string.hello_world),
// SystemClock.currentThreadTimeMillis());
Notification n = new Notification();
n.tickerText = getResources().getText(R.string.hello_world);
n.icon = R.drawable.ic_launcher;
n.defaults = Notification.DEFAULT_ALL;
Intent intent = new Intent(MainActivity.this, ClearNotification.class);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
n.setLatestEventInfo(getApplicationContext(), getResources().getText(R.string.hello_world), getResources().getText(R.string.app_name), contentIntent);
nm.notify(NOTIFICATION_ID, n);
}
});
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();

// nm.cancel(NOTIFICATION_ID);
}

 

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

 

ClearNotification.java

package com.win.notificationdemo;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class ClearNotification extends Activity {

private Button mClearBtn;
private NotificationManager nm;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

setContentView(R.layout.clear_notification);

nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

mClearBtn = (Button) findViewById(R.id.clear_button);
mClearBtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
nm.cancel(MainActivity.NOTIFICATION_ID);
}
});
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}


}

 

从网络上看了一些对于Notification对象中属性的总结,一并拿来:

audioStreamType 当声音响起时,所用的音频流的类型  

contentIntent 当通知条目被点击,就执行这个被设置的Intent.  

contentView 当通知被显示在状态条上的时候,同时这个被设置的视图被显示.  

defaults 指定哪个值要被设置成默认的.  

deleteIntent 当用户点击"Clear All Notifications"按钮区删除所有的通知的时候,这个被设置的Intent被执行.  

icon 状态条所用的图片.  

iconLevel 假如状态条的图片有几个级别,就设置这里.   

sound 通知的声音  

tickerText 通知被显示在状态条时,所显示的信息  

vibrate 振动模式

when 通知的时间戳.

 

posted @ 2013-03-09 22:16  hunterDing  阅读(177)  评论(0编辑  收藏  举报