desc:监听电池电量

main
xiaowusky 2 years ago
parent 0883ef21c7
commit 12ea3e039b

@ -12,10 +12,10 @@ android {
defaultConfig { defaultConfig {
applicationId "com.yinuo.safetywatcher" applicationId "com.yinuo.safetywatcher"
ndk { // ndk {
//SOso // //SOso
abiFilters "armeabi-v7a", "x86"//, "arm64-v8a", ,"arm64-v8a","x86_64" // abiFilters "armeabi-v7a", "x86"//, "arm64-v8a", ,"arm64-v8a","x86_64"
} // }
} }
viewBinding { viewBinding {

Binary file not shown.

@ -7,6 +7,7 @@
android:required="true" /> android:required="true" />
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.CAMERA" />
@ -18,6 +19,7 @@
android:required="true" /> android:required="true" />
<application <application
android:name=".watcher.App"
android:allowBackup="true" android:allowBackup="true"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:label="@string/app_name"

@ -0,0 +1,13 @@
package com.yinuo.safetywatcher.watcher
import android.app.Application
import com.lztek.toolkit.Lztek
import com.yinuo.safetywatcher.watcher.utils.LztekUtil
class App : Application() {
override fun onCreate() {
super.onCreate()
// LztekUtil.setObject(Lztek.create(this))
}
}

@ -6,6 +6,7 @@ import android.view.View
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import com.yinuo.safetywatcher.databinding.ActivityHomeBinding import com.yinuo.safetywatcher.databinding.ActivityHomeBinding
import com.yinuo.safetywatcher.watcher.base.BaseActivity import com.yinuo.safetywatcher.watcher.base.BaseActivity
import com.yinuo.safetywatcher.watcher.utils.BatteryHelper
class HomeActivity : BaseActivity() { class HomeActivity : BaseActivity() {
@ -19,6 +20,7 @@ class HomeActivity : BaseActivity() {
@RequiresApi(Build.VERSION_CODES.R) @RequiresApi(Build.VERSION_CODES.R)
override fun initView() { override fun initView() {
BatteryHelper.init(this@HomeActivity)
mBinding.apply { mBinding.apply {
itemSetting.setOnClickListener { itemSetting.setOnClickListener {
startActivity(Intent(this@HomeActivity, SettingActivity::class.java)) startActivity(Intent(this@HomeActivity, SettingActivity::class.java))
@ -29,4 +31,9 @@ class HomeActivity : BaseActivity() {
cameraSwitch.setOnCheckedChangeListener { buttonView, isChecked -> } cameraSwitch.setOnCheckedChangeListener { buttonView, isChecked -> }
} }
} }
override fun onDestroy() {
super.onDestroy()
BatteryHelper.release(this@HomeActivity)
}
} }

@ -0,0 +1,55 @@
package com.yinuo.safetywatcher.watcher.utils
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
import android.util.Log
object BatteryHelper {
private var callbacks = mutableListOf<OnBatteryLevelCallback>()
private var mLevel = -1;
private val receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val level = intent?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
mLevel = level!!
Log.i(this@BatteryHelper.javaClass.name, "onReceive level = $level")
callbacks.forEach {
if (level != null) {
it.onLevel(level)
}
}
}
}
fun init(context: Context) {
watchBattery(context)
}
private fun watchBattery(context: Context) {
val filter = IntentFilter(Intent.ACTION_BATTERY_CHANGED)
context.registerReceiver(receiver, filter)
}
fun addCallBack(callBack: OnBatteryLevelCallback) {
callbacks.add(callBack)
if (mLevel != -1) {
callBack.onLevel(mLevel)
}
}
fun removeCallback(callBack: OnBatteryLevelCallback) {
callbacks.remove(callBack)
}
fun release(context: Context) {
callbacks.clear()
context.unregisterReceiver(receiver)
}
interface OnBatteryLevelCallback {
fun onLevel(level: Int)
}
}

@ -0,0 +1,16 @@
package com.yinuo.safetywatcher.watcher.utils
import com.lztek.toolkit.Lztek
object LztekUtil {
private var mLztek: Lztek? = null;
fun setObject(value: Lztek) {
mLztek = value
}
fun getLztek(): Lztek {
return mLztek!!
}
}

@ -0,0 +1,49 @@
package com.yinuo.safetywatcher.watcher.utils
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.wifi.WifiManager
import android.util.Log
object WifiHelper {
private var callbacks = mutableListOf<OnWifiLevelCallback>()
private val receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val wifiState = intent?.getIntExtra(WifiManager.EXTRA_WIFI_STATE, -1)
Log.i(this@WifiHelper.javaClass.name, "onReceive wifiState = $wifiState")
callbacks.forEach {
it.onEnable(wifiState == WifiManager.WIFI_STATE_ENABLED)
}
}
}
fun init(context: Context) {
watchWifi(context)
}
private fun watchWifi(context: Context) {
val filter = IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION)
context.registerReceiver(receiver, filter)
}
fun addCallBack(callBack: OnWifiLevelCallback) {
callbacks.add(callBack)
}
fun removeCallback(callBack: OnWifiLevelCallback) {
callbacks.remove(callBack)
}
fun release(context: Context) {
callbacks.clear()
context.unregisterReceiver(receiver)
}
interface OnWifiLevelCallback {
fun onEnable(enable: Boolean)
fun onLevel(level: Int)
}
}

@ -2,13 +2,14 @@ package com.yinuo.safetywatcher.watcher.view
import android.app.Activity import android.app.Activity
import android.content.Context import android.content.Context
import android.graphics.Color
import android.opengl.Visibility
import android.util.AttributeSet import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.widget.LinearLayout import android.widget.LinearLayout
import com.yinuo.safetywatcher.R import com.yinuo.safetywatcher.R
import com.yinuo.safetywatcher.databinding.LayoutTopbarBinding import com.yinuo.safetywatcher.databinding.LayoutTopbarBinding
import com.yinuo.safetywatcher.watcher.utils.BatteryHelper
import com.yinuo.safetywatcher.watcher.utils.WifiHelper
class CommonTopBar : LinearLayout { class CommonTopBar : LinearLayout {
constructor(context: Context?) : this(context, null) constructor(context: Context?) : this(context, null)
@ -33,12 +34,47 @@ class CommonTopBar : LinearLayout {
(context as Activity).finish() (context as Activity).finish()
} }
} }
post {
// watch battery
watchBattery()
watchWifi()
}
}
private val batteryCallback = object : BatteryHelper.OnBatteryLevelCallback {
override fun onLevel(level: Int) {
Log.i(this@CommonTopBar.javaClass.name, "batteryCallback onLevel = $level")
mBinding?.battery?.visibility = if (level > 80) VISIBLE else GONE
}
}
private fun watchBattery() {
BatteryHelper.addCallBack(batteryCallback)
} }
private val wifiCallback = object : WifiHelper.OnWifiLevelCallback {
override fun onEnable(enable: Boolean) {
Log.i(this@CommonTopBar.javaClass.name, "wifiCallback onEnable = $enable")
mBinding?.wifi?.visibility = if (enable) VISIBLE else GONE
}
override fun onLevel(level: Int) {
Log.i(this@CommonTopBar.javaClass.name, "wifiCallback onEnable = $level")
}
}
private fun watchWifi() {
WifiHelper.addCallBack(wifiCallback)
}
open fun setTitle(title: String) { open fun setTitle(title: String) {
mBinding?.backArea?.visibility = VISIBLE mBinding?.backArea?.visibility = VISIBLE
mBinding?.title?.text = title mBinding?.title?.text = title
} }
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
BatteryHelper.removeCallback(batteryCallback)
}
} }
Loading…
Cancel
Save