优化:开启通知服务权限判断

This commit is contained in:
pppscn 2021-11-20 23:11:44 +08:00
parent b6a8952027
commit 21d45d717b
2 changed files with 3 additions and 47 deletions

View File

@ -1,13 +1,11 @@
package com.idormy.sms.forwarder;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.PowerManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
@ -19,7 +17,6 @@ import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
@ -204,26 +201,6 @@ public class MainActivity extends AppCompatActivity implements RefreshListView.I
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
/**
* 判断系统是否已经关闭省电优化
*
* @return boolean
*/
@RequiresApi(api = Build.VERSION_CODES.M)
private boolean isIgnoringBatteryOptimizations() {
boolean isIgnoring = false;
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (powerManager != null) {
isIgnoring = powerManager.isIgnoringBatteryOptimizations(getPackageName());
}
if (!isIgnoring) {
Intent i = new Intent(android.provider.Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
startActivity(i);
}
return isIgnoring;
}
// 初始化数据
private void initTLogs() {
logVos = LogUtil.getLog(null, null, currentType);

View File

@ -6,19 +6,16 @@ import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.BatteryManager;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationManagerCompat;
import com.idormy.sms.forwarder.MainActivity;
import com.idormy.sms.forwarder.MyApplication;
@ -26,11 +23,11 @@ import com.idormy.sms.forwarder.R;
import com.idormy.sms.forwarder.model.vo.SmsVo;
import com.idormy.sms.forwarder.sender.SendUtil;
import com.idormy.sms.forwarder.sender.SenderUtil;
import com.idormy.sms.forwarder.utils.CommonUtil;
import com.idormy.sms.forwarder.utils.PhoneUtils;
import com.idormy.sms.forwarder.utils.SettingUtil;
import java.util.Date;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
@ -112,10 +109,9 @@ public class FrontService extends Service {
}
}, 0, 10000);
if (!isNotificationListenerServiceEnabled(this)) {
openNotificationAccess();
if (SettingUtil.getSwitchEnableAppNotify() && CommonUtil.isNotificationListenerServiceEnabled(this)) {
CommonUtil.toggleNotificationListenerService(this);
}
toggleNotificationListenerService();
}
@Override
@ -154,21 +150,4 @@ public class FrontService extends Service {
}
}
private void toggleNotificationListenerService() {
PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(new ComponentName(this, NotifyService.class),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting(new ComponentName(this, NotifyService.class),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}
private static boolean isNotificationListenerServiceEnabled(Context context) {
Set<String> packageNames = NotificationManagerCompat.getEnabledListenerPackages(context);
return packageNames.contains(context.getPackageName());
}
private void openNotificationAccess() {
startActivity(new Intent(ACTION_NOTIFICATION_LISTENER_SETTINGS));
}
}