android notification和notificationmanager使用
notification和notificationmanager的使用
1、获取系统级的服务notificationmanager
//String android.content.Context.NOTIFICATION_SERVICE
String service = NOTIFICATION_SERVICE;
NotificationManager nm = (NotificationManager)getSystemService(service);
2、实例化Notification
Notification n = new Notification();
// 设置显示图标,该图标会在状态栏显示
int icon = n.icon = R.drawable.icon;
// 设置显示提示信息,该信息也会在状态栏显示
String tickerText = "Test Notification";
// 显示时间
long when = System.currentTimeMillis();
n.icon = icon;
n.tickerText = tickerText;
n.when = when;
// 也可以通过这种构造方法来设置
Notification n1 = new Notification(icon, tickerText, when);
3、实例化Intent
Intent intent = new Intent(this, MainActivity_Temp.class);
// 获得PendingIntent
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
// 设置事件信息
n.setLatestEventInfo(this, "My Title", "My Content", pi);
//具体事件 暂时不知道干什么的
n.defaults |= Notification.DEFAULT_SOUND;
n.sound = Uri.parse("file:///sdcard/sound.mp3");
n.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
n.defaults |= Notification.DEFAULT_VIBRATE;
long[] vibrate = {0,50,100,150};
n.vibrate = vibrate;
n.defaults |= Notification.DEFAULT_LIGHTS;
n.ledARGB = 0xff00ff00;
n.ledOnMS = 300;
n.ledOffMS = 1000;
n.flags |= Notification.FLAG_SHOW_LIGHTS;
4、发通知
// 标示该通知的ID
int ID = 1;
// 发出通知
nm.notify(ID, n);
利用notification和notificationmanager来实现可视化的消息显示。
package com.amaker.ch08.app;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Audio;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class TestNotificationmanager extends Activity {
// 声明按钮
private Button sendBtn,cancelBtn;
private Notification n ;
private NotificationManager nm;
// Notification标示ID
private static final int ID = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
//
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 实例化按钮
sendBtn = (Button)findViewById(R.id.sendButton01);
cancelBtn = (Button)findViewById(R.id.cancelButton02);
// 获得NotificationManager实例
String service = NOTIFICATION_SERVICE;
nm = (NotificationManager)getSystemService(service);
// 实例化Notification
n = new Notification();
// 设置显示图标,该图标会在状态栏显示
int icon = n.icon = R.drawable.happy;
// 设置显示提示信息,该信息也会在状态栏显示
String tickerText = "Test Notification";
// 显示时间
long when = System.currentTimeMillis();
n.icon = icon;
n.tickerText = tickerText;
n.when = when;
// 为按钮添加监听器
sendBtn.setOnClickListener(sendListener);
cancelBtn.setOnClickListener(cancelListener);
}
private OnClickListener sendListener = new OnClickListener(){
@Override
public void onClick(View v){
// 实例化Intent
Intent intent = new Intent(TestNotificationmanager.this, TestNotificationmanager.class);
// 获得PendingIntent
PendingIntent pi = PendingIntent.getActivity(TestNotificationmanager.this, 0, intent, 0);
// 设置事件信息
n.setLatestEventInfo(TestNotificationmanager.this, "My Title", "My Content", pi);
// n.defaults |= Notification.DEFAULT_SOUND;
// n.sound = Uri.parse("file:///sdcard/love.mp3");
// n.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
//
// n.defaults |= Notification.DEFAULT_VIBRATE;
// long[] vibrate = {0,50,100,150};
// n.vibrate = vibrate;
// n.defaults |= Notification.DEFAULT_LIGHTS;
// n.ledARGB = 0xff00ff00;
// n.ledOnMS = 300;
// n.ledOffMS = 1000;
// n.flags |= Notification.FLAG_SHOW_LIGHTS;
// 发出通知
nm.notify(ID, n);
}
};
private OnClickListener cancelListener = new OnClickListener(){
@Override
public void onClick(View v){
/// 取消通知
nm.cancel(ID);
}
};
}
在main.xml布两个按钮,在res下建一个drawable文件夹放上happy.jpg见附件
效果:
出现图标和字符通知
更多相关文章
- android camera根据屏幕图像大小设置显示
- android Paint 设置线宽setStrokeWidth()的单位
- Android(安卓)LinearLayout实现多行多列
- Android设置StatusBar颜色
- android 设置progressbar的背景颜色
- android下tcp之client测试
- android editText 自定义边框
- Android入门三 Http通信(实例)
- ANDROID画图PATH的使用