/**
* @function Notification
* @author JRwestbrook
*
*/
public class NotificationActivity extends Activity {
NotificationManager mNotificationManager;
private static final int ID_NOTIFICATION = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
// (1)获取NotificationManager对象
String ns = NOTIFICATION_SERVICE;
mNotificationManager = (NotificationManager) getSystemService(ns);
showNotification();
}
private void showNotification() {
// (2)创建一个Notification 对象
Notification notification = new Notification();
// (3)设置Notification的各个属性,如内容、图标、标题、相应的动作处理等
notification.icon = R.drawable.weixin_icon;
notification.when = System.currentTimeMillis();
String tickerText="Hello//";
tickerText=tickerText.replaceAll("//", "表情");
notification.tickerText = tickerText;// 设置在状态条(Status Bar)中显示的通知文本提示
notification.defaults |= Notification.DEFAULT_SOUND;// 设置发出的提示音
notification.defaults |= Notification.DEFAULT_VIBRATE;// 设置震动
long[] vibrate = { 0, 100, 200, 300 };
notification.vibrate = vibrate;
notification.defaults |= Notification.DEFAULT_LIGHTS;// 设置LED灯闪烁
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;// 亮的时间
notification.ledOffMS = 1000;// 灭的时间
notification.flags |= Notification.FLAG_SHOW_LIGHTS;// 设置LED灯自定义参数的方式进行闪烁
notification.flags|=notification.FLAG_AUTO_CANCEL;//点击删除
// 设置通知的事件消息
Context context = getApplicationContext();// 上下文
CharSequence contentTitle = "MyNotifacation";// 通知栏标题
CharSequence contentText = "Hello world";// 通知栏内容
Intent intent = new Intent(this, MainActivity.class);// notificationIntent
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
int requestCode = 0;
PendingIntent contentIntent = PendingIntent.getActivity(context,
requestCode, intent, 0);// flags=0
notification.setLatestEventInfo(context, contentTitle, contentText,
contentIntent);// 按以上参数值设置通知在状态栏中的属性
// (4)发送通知
// 把notification传递给notificationManager
mNotificationManager.notify(ID_NOTIFICATION, notification);
mNotificationManager.cancel(ID_NOTIFICATION);//取消前一个显示的notification,如果是一个短暂的notification,则试图隐藏,如果是一个持久的notification将从状态栏移出;
}
}
Notification(通知栏)的实现
最新推荐文章于 2023-02-08 12:23:09 发布