更新 MonitorService.java

dev
wangsiyuan 2023-12-07 19:23:14 +08:00
parent dc51082cd4
commit 4bd88021a2
1 changed files with 13 additions and 5 deletions

View File

@ -20,29 +20,34 @@ import com.nbee.echolink.utils.NetworkUtil;
import java.text.SimpleDateFormat;
import java.util.Date;
import timber.log.Timber;
public class MonitorService extends Service {
private NetworkUtil networkUtil;
private static final String TAG = "MonitorService";
private String lastPhoneNumber = null;
private long lastCallTime = 0;
private int count;
@Override
public void onCreate() {
super.onCreate();
Timber.d("MonitorService onCreate");
startForegroundService();
networkUtil = new NetworkUtil(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Timber.d("MonitorService onStartCommand");
if (intent != null) {
if (intent.hasExtra("incomingNumber")) {
String incomingNumber = intent.getStringExtra("incomingNumber");
Log.i(TAG, "获取到来电信息,号码: "+ incomingNumber);
Timber.d("获取到来电信息,号码: "+ 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);
Timber.d("获取到短信信息,号码: "+ sender);
sendSmsInfoToServer(sender, messageBody);
}
}
@ -50,6 +55,7 @@ public class MonitorService extends Service {
}
private void sendCallInfoToServer(String incomingNumber) {
Timber.d("sendCallInfoToServer: 处理来电信息");
if (incomingNumber.equals("null") || incomingNumber == null) {
return;
}
@ -57,7 +63,7 @@ public class MonitorService extends Service {
long currentTimeMillis = System.currentTimeMillis();
if (incomingNumber.equals(lastPhoneNumber) && (currentTimeMillis - lastCallTime) < 70000) {
// 如果电话号码与上次相同且在70秒之内则不执行请求
Log.i(TAG, "sendCallInfoToServer: 电话号码与上次相同且在70秒之内不执行请求.");
Timber.d("sendCallInfoToServer: 电话号码与上次相同且在70秒之内不执行请求.");
return;
}
@ -73,6 +79,7 @@ public class MonitorService extends Service {
}
private void sendSmsInfoToServer(String sender,String messageBody){
Timber.d("sendSmsInfoToServer: 处理短信信息");
if (checkNullString(sender,messageBody)){
return;
}
@ -95,6 +102,7 @@ public class MonitorService extends Service {
}
private void startForegroundService() {
Timber.d("startForegroundService: 启动前台服务");
// 创建通知频道仅在API 26及以上版本中需要
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("channel_id", "Channel Name", NotificationManager.IMPORTANCE_DEFAULT);
@ -104,8 +112,7 @@ public class MonitorService extends Service {
// 创建通知
Notification notification = new NotificationCompat.Builder(this, "channel_id")
.setContentTitle("服务运行中")
.setContentText("EchoLink正在运行")
.setContentTitle(getString(R.string.notification_title))
.setSmallIcon(R.drawable.ic_notification) // 确保您有这个图标
.build();
@ -116,6 +123,7 @@ public class MonitorService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
Timber.d("MonitorService onBind");
return null; // 不提供绑定服务的接口
}
}