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见附件

效果:
出现图标和字符通知

更多相关文章

  1. android camera根据屏幕图像大小设置显示
  2. android Paint 设置线宽setStrokeWidth()的单位
  3. Android(安卓)LinearLayout实现多行多列
  4. Android设置StatusBar颜色
  5. android 设置progressbar的背景颜色
  6. android下tcp之client测试
  7. android editText 自定义边框
  8. Android入门三 Http通信(实例)
  9. ANDROID画图PATH的使用

随机推荐

  1. enable ftrace in android kernel
  2. android之转化inputstream为list
  3. 带标题的RecyclerViewAdapter
  4. android 动态实现点击ImageButton更换图
  5. ADT20安装报错
  6. Android很nice的Spinner(NiceSpinner)
  7. Android:ListView
  8. Android重写ViewPager修改滑动灵敏度
  9. http://www.open-open.com/lib/view/open
  10. 获取手机基本信息的工具类