加入螢幕開啟/關閉偵測
This commit is contained in:
parent
a720eb9ae8
commit
37b0526e36
@ -5,6 +5,7 @@
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
||||
<uses-permission android:name="android.permission.WAKE_LOCK"/>
|
||||
|
||||
<application
|
||||
android:name=".MyApplication"
|
||||
|
||||
@ -2,9 +2,13 @@ package com.ray650128.iosclockwidget
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.*
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.os.Build
|
||||
import android.os.IBinder
|
||||
import android.util.Log
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import com.ray650128.iosclockwidget.receiver.IOSClockWidget
|
||||
@ -16,16 +20,38 @@ class ClockUpdateService : Service() {
|
||||
private val timerThread = Thread {
|
||||
while (true) {
|
||||
if (!isServiceRunning) break
|
||||
val calendar = Calendar.getInstance()
|
||||
val second = calendar[Calendar.SECOND]
|
||||
val millisecond = calendar[Calendar.MILLISECOND]
|
||||
// 計算時、分、秒的旋轉角度
|
||||
secondAngle = ((second + (millisecond / 1000f)) * 360f / 60f)//(second * 360f / 60f)
|
||||
sendWidgetIntent()
|
||||
Thread.sleep(100)
|
||||
if (isScreenOn) {
|
||||
val calendar = Calendar.getInstance()
|
||||
val second = calendar[Calendar.SECOND]
|
||||
val millisecond = calendar[Calendar.MILLISECOND]
|
||||
// 計算時、分、秒的旋轉角度
|
||||
secondAngle = ((second + (millisecond / 1000f)) * 360f / 60f)//(second * 360f / 60f)
|
||||
sendWidgetIntent()
|
||||
Thread.sleep(100)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val screenOnOffReceiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context?, intent: Intent?) {
|
||||
val action = intent?.action ?: return
|
||||
when (action) {
|
||||
Intent.ACTION_SCREEN_OFF -> isScreenOn = false
|
||||
Intent.ACTION_SCREEN_ON -> isScreenOn = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val screenOnOffIntentFilter: IntentFilter by lazy {
|
||||
IntentFilter().apply {
|
||||
this.addCategory(Intent.CATEGORY_DEFAULT)
|
||||
this.addAction(Intent.ACTION_SCREEN_ON)
|
||||
this.addAction(Intent.ACTION_SCREEN_OFF)
|
||||
}
|
||||
}
|
||||
|
||||
private var isScreenOn = true
|
||||
|
||||
override fun onBind(intent: Intent): IBinder {
|
||||
throw UnsupportedOperationException("Not yet implemented")
|
||||
}
|
||||
@ -36,6 +62,9 @@ class ClockUpdateService : Service() {
|
||||
isServiceRunning = true
|
||||
|
||||
startForeground()
|
||||
|
||||
registerReceiver(screenOnOffReceiver, screenOnOffIntentFilter)
|
||||
Log.i(TAG, "Service on, register Broadcast Receiver.")
|
||||
}
|
||||
|
||||
@SuppressLint("WakelockTimeout")
|
||||
@ -47,7 +76,9 @@ class ClockUpdateService : Service() {
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
unregisterReceiver(screenOnOffReceiver)
|
||||
isServiceRunning = false
|
||||
Log.i(TAG, "Service off, Unregister Broadcast Receiver and stop self.")
|
||||
}
|
||||
|
||||
//region Foreground Service 必要通知
|
||||
|
||||
@ -10,11 +10,15 @@ import com.ray650128.iosclockwidget.utils.PreferenceUtil
|
||||
class AutoStartReceiver : BroadcastReceiver() {
|
||||
|
||||
override fun onReceive(context: Context?, intent: Intent?) {
|
||||
Log.d("AutoStartReceiver", "intent: ${intent?.action}")
|
||||
Log.d(TAG, "intent: ${intent?.action}")
|
||||
if (context == null || intent == null) return
|
||||
if (!PreferenceUtil.isGranted) return
|
||||
val i = Intent(context, ClockUpdateService::class.java)
|
||||
context.startForegroundService(i)
|
||||
Log.d("AutoStartReceiver", "Boot completed.")
|
||||
Log.d(TAG, "Boot completed.")
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val TAG = AutoStartReceiver::class.java.simpleName
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user