zoukankan      html  css  js  c++  java
  • 检测通知栏的清除事件

    最近项目有一个需求,显示一条非常驻通知(可清除),内容可能变化,需要及时刷新。大家都知道,通知栏的刷新机制其实就是把同一个id的通知再notify一遍,这样就出现了一种情况,用户手动清除了该通知,刷新的时候,该通知又会显示出来,这时,就需要监听用户清除通知栏的事件。

        
    private Context mContext;
    private boolean isCleared=false;// 通知是否已被清除    /** * 显示/刷新通知 * @param str */ private void postTestNoti(String str) { NotificationManager notiManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); int notiId = 100; if (!isCleared) { Notification noti = new Notification(); noti.icon = R.drawable.noti_logo; noti.tickerText = mContext.getString(R.string.adhoc_activity);// 通知在title栏上滚动时显示的内容 //自定义声音 noti.sound = Uri.parse("android.resources://" + mContext.getPackageName() + "/" + R.raw.ding); //或者也可以设置默认声音 noti.defaults=Notification.DEFAULT_SOUND; long[] vibrate = { 500, 100, 500 };// 每一个数字为一个震动时限 noti.vibrate = vibrate;// 震动 noti.flags = Notification.FLAG_AUTO_CANCEL;// 点击该通知or用户点击清除,通知消失 noti.contentView = getTestNotiView(str); noti.contentIntent = getContentIntent(mContext, notiId);// 用户点击通知,跳转至TestActivity noti.deleteIntent = getDelIntent(mContext, notiId);// 用户清除通知,广播拦截 notiManager.notify(notiId, noti); } } /** * 通知的内容view * @param str * @return */ private RemoteViews getTestNotiView(String str) { RemoteViews remoteView = new RemoteViews(mContext.getPackageName(), R.layout.noti_done); remoteView.setTextViewText(R.id.noti_done_content, str); return remoteView; } /** * 点击通知后的跳转 * @param ctx * @param notiId * @return */ private PendingIntent getContentIntent(Context ctx, int notiId) { Intent intent = new Intent(ctx, AdhocActivity.class); return PendingIntent.getActivity(ctx, notiId, intent, PendingIntent.FLAG_UPDATE_CURRENT); } /** * 清除通知后的处理 * @param ctx * @param notiId * @return */ private PendingIntent getDelIntent(Context ctx, int notiId) { Intent intent = new Intent(TestBroadCast.action); return PendingIntent.getBroadcast(ctx, notiId, intent, PendingIntent.FLAG_UPDATE_CURRENT); } /** * 监听清除的广播 * @author admin * */ public class TestBroadCast extends BroadcastReceiver { public static final String action = "com.test.action.CLEAR_NOTI"; @Override public void onReceive(Context context, Intent intent) { isCleared = true; } }

    注意,要在AndroidMainfest里面定义这个广播

    <!-- 内部类用$区别于平常的. -->
            <receiver android:name="com.test.pro.notification.DownloadNotification$TestBroadCast" >
                <intent-filter>
                    <action android:name="com.test.action.CLEAR_NOTI" />
                </intent-filter>
            </receiver>

    这样,在用户点击清除的时候,广播就会捕捉到,下次调用postTestNoti刷新的时候,就不会再刷出这条通知了。

  • 相关阅读:
    VC 中与字符串相关的宏 _T、TEXT,_TEXT、L 的作用(简单明了)
    内存泄露检测工具(25款)
    谈VC++对象模型
    函数调用堆栈分析
    一个跨平台的 C++ 内存泄漏检测器
    网站架构资料收集整理
    分布式缓存系统memcached简介与实践
    反向代理(Reverse Proxy)
    squid和varnish的小结
    ajax简单后台交互
  • 原文地址:https://www.cnblogs.com/arthur3/p/3244076.html
Copyright © 2011-2022 走看看