Compare commits

...

3 Commits

Author SHA1 Message Date
wsy182 302a7dd438 Merge pull request 'master' (#1) from master into dev
Reviewed-on: #1
2024-06-26 17:43:10 +08:00
wsy182 269cbb6736 适配android 12PendingIntent. 2024-06-25 01:25:27 +08:00
halejohn 7297b8f1c4 Update MainActivity.java 2024-05-16 01:12:54 +08:00
3 changed files with 50 additions and 24 deletions

View File

@ -15,6 +15,7 @@ import com.nbee.echolink.async.BatteryOptimizationTask;
import com.nbee.echolink.service.MonitorService;
import com.nbee.echolink.service.NotificationListener;
import com.nbee.echolink.utils.BatteryOptimizationUtil;
import com.nbee.echolink.utils.DeviceInfoUtils;
import com.nbee.echolink.utils.PermissionUtils;
import com.nbee.echolink.utils.SharedPreferencesManager;
import com.nbee.echolink.utils.ShellUtils;
@ -60,6 +61,8 @@ public class MainActivity extends AppCompatActivity {
if (isFirstRun()) {
setupInitialDataAsync(); // 异步设置初始数据
spManager.putBoolean("isFirstRun", false); // 标记不再首次运行
Timber.i("首次运行,设置初始数据...");
Timber.d("android_ID: " + DeviceInfoUtils.getAndroidID(this));
}
// 请求忽略电池优化设置

View File

@ -216,8 +216,16 @@ public class MonitorService extends Service {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// 创建一个意图,指定当闹钟触发时要执行的广播接收器
Intent intent = new Intent(context, HeartbeatAlarmReceiver.class);
// 根据意图和标志位创建一个唯一的PendingIntent系统通过这个PendingIntent触发广播
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
// 根据Android版本选择合适的标志位创建一个唯一的PendingIntent
PendingIntent pendingIntent = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
// Android 12及以上版本需要指定FLAG_IMMUTABLE或FLAG_MUTABLE
pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
} else {
// 早期版本不需要指定这些标志
pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
}
// 计算心跳事件的触发间隔,单位为毫秒
long intervalMillis = HEARTBEAT_INTERVAL_MINUTES * 60 * 1000; // 10分钟的毫秒数
@ -235,4 +243,5 @@ public class MonitorService extends Service {
}
}
}

View File

@ -12,6 +12,7 @@ import com.nbee.echolink.model.WeChatMsg;
import com.nbee.echolink.utils.HandleNoticeUtils;
import com.nbee.echolink.utils.NetworkUtils;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
@ -27,6 +28,7 @@ public class NotificationListener extends NotificationListenerService {
private static final List<String> notAllowList = Arrays.asList("com.github.kr328.clash","com.google.android.dialer",
"com.google.android.apps.messaging");
private HandleNoticeUtils handleNoticeUtils;
private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
private NetworkUtils networkUtil;
private final HashMap<String, Long> recentLogs = new HashMap<>();
private final Handler logCleanerHandler = new Handler(Looper.getMainLooper());
@ -46,40 +48,52 @@ public class NotificationListener extends NotificationListenerService {
}
Bundle extras = sbn.getNotification().extras;
CharSequence titleCharSequence = extras.getCharSequence(Notification.EXTRA_TITLE);
String title = titleCharSequence != null ? titleCharSequence.toString() : null;
String content = extras.getString(Notification.EXTRA_TEXT);
String title = getNotificationText(extras, Notification.EXTRA_TITLE);
String content = getNotificationText(extras, Notification.EXTRA_TEXT);
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);
String appName = handleNoticeUtils.messageHandle(packageName);
String logMessage = String.format("packageName: %s, title: %s, content: %s, tickerText: %s", packageName, title, content, tickerText);
if ("微信".equals(appName) && tickerText.contains(":")) {
if (!shouldPrintLog(logMessage)) {
return; // 如果在30秒内已打印过则跳过
}
String appName = handleNoticeUtils.messageHandle(packageName);
if ("微信".equals(appName)) {
handleWeChatNotification(packageName, title, tickerText);
}
Timber.d(logMessage);
}
private String getNotificationText(Bundle extras, String key) {
CharSequence charSequence = extras.getCharSequence(key);
return charSequence != null ? charSequence.toString() : "";
}
private void handleWeChatNotification(String packageName, String title, String tickerText) {
if (tickerText.contains(":")) {
String[] parts = tickerText.split(":", 2);
String sender = parts[0].trim(); // 去除两端的空格
String message = parts.length > 1 ? parts[1].trim() : ""; // 同样去除两端的空格
if (parts.length < 2) return; // 安全检查,确保不会因数组越界而崩溃
String sender = parts[0].trim();
String message = parts[1].trim();
WeChatMsg weChatMsg = new WeChatMsg();
weChatMsg.setPackageName(packageName);
weChatMsg.setSender(sender);
weChatMsg.setMessage(message);
weChatMsg.setTitle(title);
weChatMsg.setAppName(appName);
weChatMsg.setCurrentTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date()));
weChatMsg.setAppName("微信");
weChatMsg.setCurrentTime(DATE_FORMAT.format(new Date()));
Timber.d("准备发送微信消息: %s", weChatMsg);
// 异步执行网络请求
networkUtil.postRequest(weChatMsg);
return;
try {
Timber.d("准备将接受的微信通知转发: %s", weChatMsg);
networkUtil.postRequest(weChatMsg);
} catch (Exception e) {
Timber.e(e, "转发送微信通知失败");
}
}
// 检查是否在30秒内已打印过相同内容
if (!shouldPrintLog(logMessage)) {
return; // 如果在30秒内已打印过则跳过
}
// Timber.d(logMessage);
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
// 当通知被移除时调用