Notification基本用法:
{ mBuilder.setContentTitle("测试标题")//设置通知栏标题
.setContentText("测试内容") //设置通知栏显示内容
.setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL)) //设置通知栏点击意图
.builder.setDeleteIntent(PendingIntent.getBroadcast(this, NOTIFICATION_ID_LIVE, new Intent(NOTIFICATION_DELETED_ACTION).putExtra(PUSH_TYPE, PUSH_TYPE_LIVE), 0)); //删除监听,需要BroadCastReceiver接收处理
.setNumber(number) //设置通知集合的数量
.setTicker("测试通知来啦") //通知首次出现在通知栏,带上升动画效果的
.setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示,一般是系统获取到的时间
.setPriority(Notification.PRIORITY_DEFAULT) //设置该通知优先级
// .setAutoCancel(true) //用户单击通知就会让通知自动消失,
.setOngoing(false)//ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
.setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合
//Notification.DEFAULT_ALL Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission
.setSmallIcon(R.drawable.ic_launcher);//设置通知小ICON
}
public class ButtonBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(NOTIFICATION_DELETED_ACTION)) {
。。。
}
}
}
}
Notification细节参考地址:http://blog.csdn.net/vipzjyno1/article/details/25248021
Notification自定义布局:
public void initAndNotify(String appName, String networkSpeed, String currentSize, String totalSize) {
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
// 此处设置的图标仅用于显示新提醒时候出现在设备的通知栏
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
notification = mBuilder.build();
// 当用户下来通知栏时候看到的就是RemoteViews中自定义的Notification布局
contentView = new RemoteViews(context.getPackageName(), R.layout.progress_bar_layout);
//设置通知栏信息
contentView.setTextViewText(R.id.txt_appName, appName);//应用名称
contentView.setTextViewText(R.id.txt_network_speed, networkSpeed + "kb/s");//当前网速
StringBuffer stringBuffer = new StringBuffer(currentSize);
stringBuffer.append("/");
stringBuffer.append(totalSize);
stringBuffer.append("M");
contentView.setTextViewText(R.id.txt_size_progress, stringBuffer.toString());//当前下载进度
//如果版本号低于(3.0),那么不显示按钮
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
contentView.setViewVisibility(R.id.btn_download, View.GONE);
} else {
//注册广播
receiver = new ButtonBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_BUTTON);
context.registerReceiver(receiver, intentFilter);
//设置按钮
contentView.setImageViewResource(R.id.btn_download, R.drawable.btn_notification_play);
//设置点击的事件
Intent buttonIntent = new Intent(ACTION_BUTTON);
buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_PALY_ID);
PendingIntent intent_paly = PendingIntent.getBroadcast(context, BUTTON_PALY_ID, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT);
contentView.setOnClickPendingIntent(R.id.btn_download, intent_paly);
}
notification.contentView = contentView;
// 点击notification自动消失
notification.flags = Notification.FLAG_AUTO_CANCEL;
// 需要注意的是,作为选项,此处可以设置MainActivity的启动模式为singleTop,避免重复新建onCreate()。
Intent intent = new Intent(context, MainActivity.class);
// 当用户点击通知栏的Notification时候,切换回TaskDefineActivity。
PendingIntent pi = PendingIntent.getActivity(context, REQUEST_CODE,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.contentIntent = pi;
// 发送到手机的通知栏
notificationManager.notify(NOTIFICATION_ID, notification);
}
/**
* (通知栏中的点击事件是通过广播来通知的,所以在需要处理点击事件的地方注册广播即可)
* 广播监听按钮点击事件
*/
public class ButtonBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(ACTION_BUTTON)) {
//通过传递过来的ID判断按钮点击属性或者通过getResultCode()获得相应点击事件
int buttonId = intent.getIntExtra(INTENT_BUTTONID_TAG, 0);
switch (buttonId) {
case BUTTON_PALY_ID:
break;
default:
break;
}
}
}
}
/**
* 关闭通知
*/
public void cancel() {
if (receiver != null) {
context.unregisterReceiver(receiver);
}
if (notificationManager != null) {
notificationManager.cancel(NOTIFICATION_ID);
}
}
参考网址:http://blog.csdn.net/cai_iac/article/details/51811512
.
网友评论