更新 NotificationListener.java
parent
172a26896d
commit
3e284d528e
|
|
@ -2,6 +2,8 @@ package com.nbee.echolink.service;
|
||||||
|
|
||||||
import android.app.Notification;
|
import android.app.Notification;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.Looper;
|
||||||
import android.service.notification.NotificationListenerService;
|
import android.service.notification.NotificationListenerService;
|
||||||
import android.service.notification.StatusBarNotification;
|
import android.service.notification.StatusBarNotification;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
@ -13,6 +15,7 @@ import com.nbee.echolink.utils.NetworkUtil;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
|
|
@ -25,6 +28,8 @@ public class NotificationListener extends NotificationListenerService {
|
||||||
"com.google.android.apps.messaging");
|
"com.google.android.apps.messaging");
|
||||||
private HandleNoticeUtils handleNoticeUtils;
|
private HandleNoticeUtils handleNoticeUtils;
|
||||||
private NetworkUtil networkUtil;
|
private NetworkUtil networkUtil;
|
||||||
|
private HashMap<String, Long> recentLogs = new HashMap<>();
|
||||||
|
private Handler logCleanerHandler = new Handler(Looper.getMainLooper());
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
|
|
@ -44,15 +49,24 @@ public class NotificationListener extends NotificationListenerService {
|
||||||
String title = extras.getString(Notification.EXTRA_TITLE);
|
String title = extras.getString(Notification.EXTRA_TITLE);
|
||||||
String content = extras.getString(Notification.EXTRA_TEXT);
|
String content = extras.getString(Notification.EXTRA_TEXT);
|
||||||
String tickerText = sbn.getNotification().tickerText != null ? sbn.getNotification().tickerText.toString() : "";
|
String tickerText = sbn.getNotification().tickerText != null ? sbn.getNotification().tickerText.toString() : "";
|
||||||
|
String logMessage = String.format("packageName: %s,title: %s,content: %s,tickerText: %s", packageName, title, content, tickerText);
|
||||||
|
// 检查是否在30秒内已打印过相同内容
|
||||||
|
if (!shouldPrintLog(logMessage)) {
|
||||||
|
return; // 如果在30秒内已打印过,则跳过
|
||||||
|
}
|
||||||
|
|
||||||
|
Timber.d(logMessage);
|
||||||
Timber.d("packageName: %s,title: %s,content: %s,tickerText: %s",packageName,title,content,tickerText);
|
Timber.d("packageName: %s,title: %s,content: %s,tickerText: %s",packageName,title,content,tickerText);
|
||||||
|
|
||||||
String appName = handleNoticeUtils.messageHandle(packageName);
|
String appName = handleNoticeUtils.messageHandle(packageName);
|
||||||
if ("微信".equals(appName) && tickerText.contains(":")) {
|
if ("微信".equals(appName) && tickerText.contains(":")) {
|
||||||
String[] parts = tickerText.split(":", 2);
|
String[] parts = tickerText.split(":", 2);
|
||||||
|
String sender = parts[0].trim(); // 去除两端的空格
|
||||||
|
String message = parts.length > 1 ? parts[1].trim() : ""; // 同样去除两端的空格
|
||||||
WeChatMsg weChatMsg = new WeChatMsg();
|
WeChatMsg weChatMsg = new WeChatMsg();
|
||||||
weChatMsg.setPackageName(packageName);
|
weChatMsg.setPackageName(packageName);
|
||||||
weChatMsg.setSender(parts[0]);
|
weChatMsg.setSender(sender);
|
||||||
weChatMsg.setMessage(parts.length > 1 ? parts[1] : "");
|
weChatMsg.setMessage(message);
|
||||||
weChatMsg.setTitle(title);
|
weChatMsg.setTitle(title);
|
||||||
weChatMsg.setAppName(appName);
|
weChatMsg.setAppName(appName);
|
||||||
weChatMsg.setCurrentTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date()));
|
weChatMsg.setCurrentTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date()));
|
||||||
|
|
@ -60,8 +74,6 @@ public class NotificationListener extends NotificationListenerService {
|
||||||
Timber.d("准备发送微信消息: %s", weChatMsg);
|
Timber.d("准备发送微信消息: %s", weChatMsg);
|
||||||
// 异步执行网络请求
|
// 异步执行网络请求
|
||||||
networkUtil.postRequest(weChatMsg);
|
networkUtil.postRequest(weChatMsg);
|
||||||
} else {
|
|
||||||
Timber.d("非微信通知或TickerText格式不正确,已忽略");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -71,4 +83,24 @@ public class NotificationListener extends NotificationListenerService {
|
||||||
// 当通知被移除时调用
|
// 当通知被移除时调用
|
||||||
Log.d(TAG, "通知被移除: " + sbn.getPackageName());
|
Log.d(TAG, "通知被移除: " + sbn.getPackageName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查是否应打印日志
|
||||||
|
*/
|
||||||
|
private boolean shouldPrintLog(String logMessage) {
|
||||||
|
Long lastPrintTime = recentLogs.get(logMessage);
|
||||||
|
long currentTime = System.currentTimeMillis();
|
||||||
|
|
||||||
|
if (lastPrintTime == null || (currentTime - lastPrintTime) > 30000) {
|
||||||
|
// 更新日志的打印时间
|
||||||
|
recentLogs.put(logMessage, currentTime);
|
||||||
|
|
||||||
|
// 安排一分钟后清理这个日志条目
|
||||||
|
logCleanerHandler.postDelayed(() -> recentLogs.remove(logMessage), 60000);
|
||||||
|
|
||||||
|
return true; // 如果没有打印过或距离上次打印超过30秒,则应打印
|
||||||
|
}
|
||||||
|
|
||||||
|
return false; // 如果在30秒内已打印过,则不打印
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue