更新 MainActivity.java
parent
b013fa64d1
commit
4cd24ebe02
|
|
@ -1,39 +1,52 @@
|
||||||
package com.nbee.echolink;
|
package com.nbee.echolink;
|
||||||
|
|
||||||
|
import static java.lang.Thread.*;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.PowerManager;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
|
||||||
import com.nbee.echolink.service.MonitorService;
|
import com.nbee.echolink.service.MonitorService;
|
||||||
|
import com.nbee.echolink.utils.BatteryOptimizationUtil;
|
||||||
import com.nbee.echolink.utils.PermissionUtils;
|
import com.nbee.echolink.utils.PermissionUtils;
|
||||||
|
import com.nbee.echolink.utils.ShellUtils;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import timber.log.Timber;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
private final String TAG = "MainActivity";
|
private final String TAG = "MainActivity";
|
||||||
private PermissionUtils permissionUtils;
|
private PermissionUtils permissionUtils;
|
||||||
private TextView logTextView; // 将类型从 View 更改为 TextView
|
private TextView logTextView; // 将类型从 View 更改为 TextView
|
||||||
|
private ShellUtils shellUtils;
|
||||||
|
private boolean isRoot;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
|
|
||||||
|
isRoot = shellUtils.checkRootPermission();
|
||||||
|
if (!isRoot){
|
||||||
|
Timber.d("未获取root权限: "+ isRoot);
|
||||||
|
}
|
||||||
logTextView = findViewById(R.id.logTextView);
|
logTextView = findViewById(R.id.logTextView);
|
||||||
updateLog("初始化日志...");
|
updateLog("初始化日志...");
|
||||||
|
|
||||||
|
requestIgnoreBattery();
|
||||||
|
|
||||||
permissionUtils = new PermissionUtils(this);
|
permissionUtils = new PermissionUtils(this);
|
||||||
permissionUtils.checkPermissions();
|
permissionUtils.checkPermissions();
|
||||||
|
|
||||||
// 启动MonitorService服务
|
// 启动MonitorService服务
|
||||||
|
|
||||||
Intent serviceIntent = new Intent(this, MonitorService.class);
|
Intent serviceIntent = new Intent(this, MonitorService.class);
|
||||||
startService(serviceIntent);
|
startService(serviceIntent);
|
||||||
}
|
}
|
||||||
|
|
@ -50,7 +63,17 @@ public class MainActivity extends AppCompatActivity {
|
||||||
String logContent = readLogFile();
|
String logContent = readLogFile();
|
||||||
updateLog(logContent);
|
updateLog(logContent);
|
||||||
}
|
}
|
||||||
|
private void requestIgnoreBattery(){
|
||||||
|
Context context = getApplicationContext();
|
||||||
|
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
|
||||||
|
boolean hasIgnored = powerManager.isIgnoringBatteryOptimizations(context.getPackageName());
|
||||||
|
if(!hasIgnored){
|
||||||
|
BatteryOptimizationUtil.requestIgnoreBatteryOptimization(context);
|
||||||
|
Timber.d("不在在优化电池白名单内");
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void updateLog(String newLog) {
|
private void updateLog(String newLog) {
|
||||||
logTextView.setText(newLog);
|
logTextView.setText(newLog);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue