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