这里只是简单地监控微信红包消息,并不能主动抢红包,主动抢红包从技术上来说是有可能的,需要通过网络抓包分析抢红包接口信息,稍有复杂。监控的原理也很简单实时监听消息栏的微信消息(如果你屏蔽了某群的消息,那自然就监控不了),带有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;
}
}
}
未完待续。。。