Commit 528765db authored by wanglei's avatar wanglei

...

parent ab3f6ff0
Pipeline #1384 canceled with stages
...@@ -19,5 +19,6 @@ public class Global { ...@@ -19,5 +19,6 @@ public class Global {
public static String lockNotificationStatus = "lockNotificationStatus"; public static String lockNotificationStatus = "lockNotificationStatus";
public static String batteryNotificationStatus = "batteryNotificationStatus";
} }
...@@ -4,17 +4,13 @@ import android.content.BroadcastReceiver ...@@ -4,17 +4,13 @@ import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.BatteryManager import android.os.BatteryManager
import android.os.Build
import android.util.Log import android.util.Log
import androidx.annotation.RequiresApi import com.swiftcleaner.chovey.model.bean.Global.batteryNotificationStatus
import com.zxdemo.notity.NotificationUtils import com.zxdemo.notity.NotificationUtils
import com.zxdemo.utils.SpUtils import com.zxdemo.utils.SpUtils
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit
class BatteryStatusReceiver() : BroadcastReceiver() { class BatteryStatusReceiver() : BroadcastReceiver() {
@RequiresApi(Build.VERSION_CODES.O)
override fun onReceive(context: Context?, batteryIntent: Intent?) { override fun onReceive(context: Context?, batteryIntent: Intent?) {
val level = batteryIntent?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) ?: -1 val level = batteryIntent?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) ?: -1
val scale = batteryIntent?.getIntExtra(BatteryManager.EXTRA_SCALE, -1) ?: -1 val scale = batteryIntent?.getIntExtra(BatteryManager.EXTRA_SCALE, -1) ?: -1
...@@ -27,24 +23,17 @@ class BatteryStatusReceiver() : BroadcastReceiver() { ...@@ -27,24 +23,17 @@ class BatteryStatusReceiver() : BroadcastReceiver() {
val voltage = batteryIntent?.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1) ?: -1 val voltage = batteryIntent?.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1) ?: -1
val temperature = batteryIntent?.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1) ?: -1 val temperature = batteryIntent?.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1) ?: -1
val technology = batteryIntent?.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY) ?: "Unknown" val technology = batteryIntent?.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY) ?: "Unknown"
val batteryPct = level * 100 / scale.toFloat() val batteryPct = level * 100 / scale.toFloat()
val intervalTime = SpUtils.getInstance().getInt("batteryNotificationInterval", 5) var batteryNotificationStatus = SpUtils.getInstance().getInt(batteryNotificationStatus, 0)
var current = LocalDateTime.now()
var lastTime =
SpUtils.getInstance().getString("NOTICE_BATTERY_TIME", "")
var parsedDateTime = current
if (lastTime != "") {
parsedDateTime =
LocalDateTime.parse(lastTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"))
}
var batteryNotificationStatus = SpUtils.getInstance().getInt("batteryNotificationStatus", 0)
val minutesBetween = ChronoUnit.MINUTES.between(parsedDateTime, current)
// Log.d("TAG", "onReceive:电池 $minutesBetween $intervalTime")
if ((batteryNotificationStatus == 1 && (minutesBetween > intervalTime && minutesBetween.toInt() == 0)) && (state == BatteryManager.BATTERY_STATUS_CHARGING || (level < 20 && level > 10))) { if (batteryNotificationStatus == 1
&&
(level in 11..19)
&&
state == BatteryManager.BATTERY_STATUS_CHARGING
) {
Log.d("TAG", "onReceive: 电池推送") Log.d("TAG", "onReceive: 电池推送")
if (context != null) { if (context != null) {
NotificationUtils.sendNotification( NotificationUtils.sendNotification(
...@@ -52,10 +41,6 @@ class BatteryStatusReceiver() : BroadcastReceiver() { ...@@ -52,10 +41,6 @@ class BatteryStatusReceiver() : BroadcastReceiver() {
"showNotification", "showNotification",
"receive_battery" "receive_battery"
) )
SpUtils.getInstance().putString(
"NOTICE_BATTERY_TIME",
current.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"))
)
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment