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