first commit
This commit is contained in:
121
app/src/main/java/com/nbee/echolink/service/MonitorService.java
Normal file
121
app/src/main/java/com/nbee/echolink/service/MonitorService.java
Normal file
@@ -0,0 +1,121 @@
|
||||
package com.nbee.echolink.service;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.Service;
|
||||
import android.content.Intent;
|
||||
import android.os.Build;
|
||||
import android.os.IBinder;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.core.app.NotificationCompat;
|
||||
|
||||
import com.nbee.echolink.R;
|
||||
import com.nbee.echolink.model.CallInfo;
|
||||
import com.nbee.echolink.model.SMSInfo;
|
||||
import com.nbee.echolink.utils.NetworkUtil;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class MonitorService extends Service {
|
||||
private NetworkUtil networkUtil;
|
||||
private static final String TAG = "MonitorService";
|
||||
private String lastPhoneNumber = null;
|
||||
private long lastCallTime = 0;
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
startForegroundService();
|
||||
networkUtil = new NetworkUtil(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
if (intent != null) {
|
||||
if (intent.hasExtra("incomingNumber")) {
|
||||
String incomingNumber = intent.getStringExtra("incomingNumber");
|
||||
Log.i(TAG, "获取到来电信息,号码: "+ incomingNumber);
|
||||
sendCallInfoToServer(incomingNumber);
|
||||
} else if (intent.hasExtra("sender") && intent.hasExtra("messageBody")) {
|
||||
String sender = intent.getStringExtra("sender");
|
||||
String messageBody = intent.getStringExtra("messageBody");
|
||||
Log.i(TAG, "获取到短信信息,号码: "+ sender);
|
||||
sendSmsInfoToServer(sender, messageBody);
|
||||
}
|
||||
}
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
private void sendCallInfoToServer(String incomingNumber) {
|
||||
if (incomingNumber.equals("null") || incomingNumber == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
long currentTimeMillis = System.currentTimeMillis();
|
||||
if (incomingNumber.equals(lastPhoneNumber) && (currentTimeMillis - lastCallTime) < 70000) {
|
||||
// 如果电话号码与上次相同,且在70秒之内,则不执行请求
|
||||
Log.i(TAG, "sendCallInfoToServer: 电话号码与上次相同,且在70秒之内,不执行请求.");
|
||||
return;
|
||||
}
|
||||
|
||||
// 更新存储的电话号码和时间戳
|
||||
lastPhoneNumber = incomingNumber;
|
||||
lastCallTime = currentTimeMillis;
|
||||
String currentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
|
||||
|
||||
CallInfo callInfo = new CallInfo();
|
||||
callInfo.setCallTime(currentTime);
|
||||
callInfo.setPhoneNumber(incomingNumber);
|
||||
networkUtil.postRequest(callInfo);
|
||||
}
|
||||
|
||||
private void sendSmsInfoToServer(String sender,String messageBody){
|
||||
if (checkNullString(sender,messageBody)){
|
||||
return;
|
||||
}
|
||||
String currentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
|
||||
SMSInfo smsInfo = new SMSInfo();
|
||||
smsInfo.setSmsNumber(sender);
|
||||
smsInfo.setSmsContent(messageBody);
|
||||
smsInfo.setSmsAcceptanceTime(currentTime);
|
||||
networkUtil.postRequest(smsInfo);
|
||||
}
|
||||
|
||||
private boolean checkNullString(String a, String b){
|
||||
if (a == null || b == null){
|
||||
return true;
|
||||
}
|
||||
if (a.equals("null") || b.equals("null")){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void startForegroundService() {
|
||||
// 创建通知频道(仅在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("服务运行中")
|
||||
.setContentText("EchoLink正在运行")
|
||||
.setSmallIcon(R.drawable.ic_notification) // 确保您有这个图标
|
||||
.build();
|
||||
|
||||
// 启动前台服务
|
||||
startForeground(1, notification);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null; // 不提供绑定服务的接口
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user