监控微信红包消息(android)

这里只是简单地监控微信红包消息,并不能主动抢红包,主动抢红包从技术上来说是有可能的,需要通过网络抓包分析抢红包接口信息,稍有复杂。监控的原理也很简单实时监听消息栏的微信消息(如果你屏蔽了某群的消息,那自然就监控不了),带有xxx[微信红包]xxx 的微信消息认为有红包可抢,此时记录消息发送者,发送时间,并同时提醒用户。当消息被点开后,通知栏消息消失根据消息的时间和id,认为此红包被处理。主要类为NotificationListenerService,系统版本需要>=4.3

public class SimpleKitkatNotificationListener extends
        NotificationListenerService {
    private String tag = "SimpleKitkatNotificationListener";
    private Notification notifi;
    private NotificationManager nm;
    private boolean hasInited = false;
    Map<String, List<String>> qunMap = new HashMap<String, List<String>>();

    @Override
    public void onCreate() {
        super.onCreate();
        // android.os.Debug.waitForDebugger();
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        Notification mNotification = sbn.getNotification();
        String postTime = String.valueOf(sbn.getPostTime());
        Log.e(tag, postTime + "时发来:");
        if (mNotification != null) {
            Bundle extras = mNotification.extras;
            String text = extras.getCharSequence(Notification.EXTRA_TEXT)
                    .toString();
            if (text.contains("[微信红包]")) {
                // createNotification();

                String qunStr = extras
                        .getCharSequence(Notification.EXTRA_TITLE).toString();
                Log.e(tag, qunStr + postTime + "时发来:" + text);
                if (qunMap.containsKey(qunStr)) {
                    List<String> list = qunMap.get(qunStr);
                    list.add(postTime);

                } else {
                    List<String> list = new ArrayList<String>();
                    list.add(postTime);
                    qunMap.put(qunStr, list);
                }
            }
        }
        createNotification(true);

    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {

        String postTime = String.valueOf(sbn.getPostTime());
        Notification mNotification = sbn.getNotification();
        if (mNotification != null) {
            Bundle extras = mNotification.extras;
            String text = extras.getCharSequence(Notification.EXTRA_TEXT)
                    .toString();
            if (text.contains("[微信红包]")) {
                String qunStr = extras
                        .getCharSequence(Notification.EXTRA_TITLE).toString();
                Log.e(tag, qunStr + postTime + "时查看:" + text);
                if (qunMap.containsKey(qunStr)) {
                    List<String> list = qunMap.get(qunStr);
                    if (list != null) {
                        list.remove(postTime);
                        if (list.isEmpty()) {
                            qunMap.remove(qunStr);
                        }
                    } else {
                        qunMap.remove(qunStr);
                    }
                }
                MyApp app = (MyApp) getApplication();
                app.setMap(qunMap);
                createNotification(false);
            }
        }

    }

    private void createNotification(boolean isPost) {
        if (hasInited) {
            updateNotification(isPost);
        } else {
            hasInited = true;
            NotificationCompat.Builder builder = new NotificationCompat.Builder(
                    this);
            nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            builder.setAutoCancel(true);
            builder.setOngoing(true);
            // builder.
            notifi = builder.build();
            notifi.flags = Notification.DEFAULT_ALL;
            Intent intent = new Intent();
            intent.setClass(getApplicationContext(), MainActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(
                    this.getApplicationContext(), 0, intent, 0);
            notifi.contentIntent = pendingIntent;
            notifi.when = System.currentTimeMillis();
            notifi.number = 1;
            notifi.icon = R.drawable.ic_launcher;
            notifi.tickerText = "有红包啦";
            nm.notify(R.string.app_name, notifi);
        }
    }

    private void updateNotification(boolean isPost) {
        if (notifi.number >= 1) {
            notifi.tickerText = "又有红包啦";
        } else {
            notifi.tickerText = "红包来啦";
        }
        Intent intent = new Intent();
        intent.setClass(getApplicationContext(), MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(
                this.getApplicationContext(), 0, intent, 0);
        notifi.contentIntent = pendingIntent;
        notifi.when = System.currentTimeMillis();
        if (isPost) {
            notifi.number += 1;
        } else
            notifi.number -= 1;

        notifi.flags = Notification.DEFAULT_ALL;
        nm.notify(R.string.app_name, notifi);
        if (notifi.number == 0) {
            nm.cancelAll();
            hasInited = false;
        }
    }
}

未完待续。。。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值