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 notification和notificationmanager使用出现图标和字符通知

更多相关文章

  1. Android的Intent+uri应用实例
  2. Android入门三 Http通信(实例)
  3. Android 之通知Notification应用
  4. android 使用意图(Intent)实现一键拨号实例
  5. Android通知栏图标显示网络图片
  6. Android判断app是否打开消息通知并跳转设置
  7. Android 数据存储(数据库、文件、参数)操作实例
  8. Android之ListView与自定义adapter简单实例
  9. Android sqlite 基础实例

随机推荐

  1. 侧滑栏DrawerLayout的简单使用
  2. android沉浸式+虚拟按键+Fragment+Coordi
  3. android Launcher基础知识
  4. android GPS数据读取
  5. Android USBCamera,UVCCamera开发通用库,我
  6. Android(安卓)onConfigureChanges 是如何
  7. android使用PullToRefresh框架实现ListVi
  8. Android本地数据存储之.txt文件存储读写
  9. 使用Kotlin开发Android 扩展函数(Extensio
  10. react-native 使用android DownloadManag