自定义下拉通知栏,一键删除功能

时间:2022-08-30 16:54:27


1.分析

状态通知栏主要涉及到2个类:  Notification 和 NotificationManager 

Notification为通知信息类,它里面对应了通知栏的各个属性

NotificationManager :  是状态通知的管理类,负责发通知、清除通知等操作。

注意:NotificationManager 是一个系统Service,所以必须通过 getSystemService(NOTIFICATION_SERVICE)方法来获取,方法如下。

:NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  

2.流程

第一步:

创建一个通知栏的Builder构造类  (Create a Notification Builder)

第二步:

定义通知栏的Action  (Define the Notification's Action)

第三步:

设置通知栏点击事件    (Set the Notification's Click Behavior)

第四步:

通知   (Issue the Notification)

3.代码

实现系统默认的通知栏效果:

第一步:获取状态通知栏管理


NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 


第二步:实例化通知栏构造器NotificationCompat.Builder:


NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)


第三步:对Builder进行配置:


  1. mBuilder.setContentTitle("测试标题")//设置通知栏标题  
  2.     .setContentText("测试内容") /<span style="font-family: Arial;">/设置通知栏显示内容</span>  
  3.     .setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL)) //设置通知栏点击意图  
  4. //  .setNumber(number) //设置通知集合的数量  
  5.     .setTicker("测试通知来啦"//通知首次出现在通知栏,带上升动画效果的  
  6.     .setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示,一般是系统获取到的时间  
  7.     .setPriority(Notification.PRIORITY_DEFAULT) //设置该通知优先级  
  8. //  .setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消    
  9.     .setOngoing(false)//ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)  
  10.     .setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合  
  11.     //Notification.DEFAULT_ALL  Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission  
  12.     .setSmallIcon(R.drawable.ic_launcher);//设置通知小ICON


第四步:RemoteViews 自定义 view


RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification);
remoteViews.setTextViewText(R.id.tv_up, "测试标题");
remoteViews.setTextViewText(R.id.tv_down, "测试文本");

Intent intent = new Intent(ACTION_BTN);
intent.putExtra(INTENT_NAME, INTENT_BTN_LOGIN);


设置不同区域的点击的pendingIntent

PendingIntent intentpi = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.btn_login, intentpi);

Intent intent2 = new Intent();
intent2.setClass(this, MainActivity.class);
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent intentContent = PendingIntent.getActivity(this, 0, intent2, PendingIntent.FLAG_UPDATE_CURRENT);


remoteViews.setOnClickPendingIntent(R.id.close_iv,intentContent);


默认的Notification只能通过setContentIntent设置整体的点击事件。不过通过RemoteViews我们可以设置不同地方不同的点击事件,当然这里的事件指的是PendingIntent。如下,设置了点击R.id.notice_view_type_0打开一个Activity,而点击R.id.close_iv会发出一个广播,可以通过这个广播的广播接收器来做一些事情,如这里是关闭当前的Notification。另外还可以打开一个Service。

《--------------------------------------------------我是分割线------------------------------------------------------------------------》

下面贴上测试代码


public class TestNotifition {

public final static String ACTION_BTN = "com.example.notification.btn.login";

public final static String INTENT_NAME = "btnid";

public final static int INTENT_BTN_LOGIN = 1;

NotificationBroadcastReceiver mReceiver;

public void notification(Context context) {
unregeisterReceiver(context);
intiReceiver(context);

// FmService mFmService = new FmService();
// FmService.LocalStation mStation= mFmService.new LocalStation(context);

RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.notification);
// remoteViews.setTextViewText(R.id.tv_up, mStation.isFm() ? "FM" : "AM");
remoteViews.setTextViewText(R.id.tv_up, "FM");
remoteViews.setTextViewText(R.id.tv_down, "87.5");

Intent intent = new Intent(ACTION_BTN);
intent.putExtra(INTENT_NAME, INTENT_BTN_LOGIN);
PendingIntent intentpi = PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.btn_login, intentpi);

Intent intent2 = new Intent();
intent2.setClass(context, SqrFmMainActivity.class);
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
PendingIntent intentContent = PendingIntent.getActivity(context, 0, intent2, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

builder.setOngoing(false);
builder.setAutoCancel(true);
builder.setContent(remoteViews);
builder.setTicker("正在使用FM电台收听");
builder.setSmallIcon(R.drawable.ic_launcher);//通知图标
builder.setPriority(NotificationCompat.PRIORITY_HIGH);//通知优先级
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.searchbarbtn3);
builder.setLargeIcon(bitmap);
builder.setNumber(1024);
builder.setWhen(System.currentTimeMillis());

Notification notification = builder.build();
// notification.defaults = Notification.DEFAULT_SOUND;
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.contentIntent = intentContent;

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}

public void intiReceiver(Context context) {
mReceiver = new NotificationBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_BTN);
context.getApplicationContext().registerReceiver(mReceiver, intentFilter);
}

public void unregeisterReceiver(Context context) {
if (mReceiver != null) {
context.getApplicationContext().unregisterReceiver(mReceiver);
mReceiver = null;
}
}

public class NotificationBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(ACTION_BTN)) {
int btn_id = intent.getIntExtra(INTENT_NAME, 0);
switch (btn_id) {
case INTENT_BTN_LOGIN:
Toast.makeText(context, "红红火火恍恍惚惚哈哈哈哈", Toast.LENGTH_SHORT).show();
unregeisterReceiver(context);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(0);
break;
}
}
}
}

}



更多详情 http://blog.csdn.net/vipzjyno1/article/details/25248021

   http://www.open-open.com/lib/view/open1460988804855.html