notificationManager = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE); notification = new Notification(R.drawable.touxiang,"信息",System.currentTimeMillis());
sendButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(NotificationActivity.this,NotificationActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(NotificationActivity.this, 0, intent, 0);
notification.setLatestEventInfo(NotificationActivity.this, "你的一条信息", "来自张三的信息", pendingIntent);
notificationManager.notify(ID,notification);
notificationManager.notify(ID+1, notification);
}
})
package com.study.android;
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.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button startBtn;
private Button cancelBtn;
private static final int HELLO_ID = 1;
NotificationManager mNotificationManager;
Notification mNotification;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startBtn = (Button)findViewById(R.id.startBtn);
cancelBtn = (Button)findViewById(R.id.cancelBtn);
// ① 获取NotificationManager的引用
String ns = Context.NOTIFICATION_SERVICE;
mNotificationManager = (NotificationManager)this.getSystemService(ns);
// ② 初始化Notification
int icon = R.drawable.ic_launcher;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();
mNotification = new Notification(icon,tickerText,when);
mNotification.defaults = Notification.DEFAULT_ALL;
mNotification.flags |= Notification.FLAG_NO_CLEAR;
mNotification.flags |= Notification.FLAG_SHOW_LIGHTS;
// ③ 定义notification的消息 和 PendingIntent
Context context = this;
CharSequence contentTitle ="My notification";
CharSequence contentText = "Hello World";
Intent notificationIntent = new Intent(this,MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,0 );
mNotification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
// ④ 把封装好的notification传入NotificationManager
// 开启通知
startBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mNotificationManager.notify(HELLO_ID,mNotification);
}
});
// 取消通知
cancelBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mNotificationManager.cancel(HELLO_ID);
}
});
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有