Как сделать приложение которое блокирует телефон (не дает войти в телефон) в промежутке времени
Кто нибудь может подсказать как сделать приложение которое блокирует телефон (не дает войти в телефон) в промежутке времени? к примеру пользователь не хочет чтобы там его жена или кто-то еще смотрел в его телефон пока его нет или он спит и мы в приложений ставим с какого времени заблокировать и когда разблокировать (но мне нужно понять как мне его заблокировать и разблокировать)! Спасибо заранее!
Мой сервис
package app.beer.wacklock
import android.app.Service
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.Color
import android.graphics.PixelFormat
import android.os.Build
import android.os.IBinder
import android.os.PowerManager.WakeLock
import android.util.Log
import android.view.View
import android.view.WindowManager
const val WAKE_LOCK_TAG = "my_wake_lock_tag"
class LockService : Service() {
lateinit var wl: WakeLock
var mWindowManager: WindowManager? = null
var isShowing = false
var mView: View? = null
var mThread: Thread? = null
var mReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent) {
val action = intent.action
if (action == Intent.ACTION_SCREEN_ON) {
if (isShowing) {
if (mThread == null || mThread != null && !mThread!!.isAlive)
mThread = object : Thread() {
override fun run() {
try {
sleep(1000)
mWindowManager!!.removeView(mView)
isShowing = false
} catch (e: InterruptedException) {
e.printStackTrace()
}
}
}
mThread!!.start()
}
}
if (action == Intent.ACTION_SCREEN_OFF) {
if (!isShowing) {
// val params = WindowManager.LayoutParams(
// WindowManager.LayoutParams.MATCH_PARENT,
// WindowManager.LayoutParams.MATCH_PARENT,
// WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
// WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
// or // Draws over status bar
// WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
// PixelFormat.TRANSLUCENT
// )
val layout_parms: Int = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
} else {
WindowManager.LayoutParams.TYPE_PHONE
}
val params = WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
layout_parms,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE or WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT
)
mWindowManager!!.addView(mView, params)
isShowing = true
}
}
}
}
override fun onBind(intent: Intent?): IBinder? {
return null
}
override fun onCreate() {
mView = View(this)
mView?.setBackgroundColor(Color.GREEN)
mWindowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
// val pm = (getSystemService(Context.POWER_SERVICE) as PowerManager).also {
// wl = it.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKE_LOCK_TAG)
// }
// wl.acquire(10 * 60 * 1000L)
val intentFilter = IntentFilter()
intentFilter.addAction(Intent.ACTION_SCREEN_ON)
intentFilter.addAction(Intent.ACTION_SCREEN_OFF)
//intentFilter.addAction(Intent.ACTION_USER_PRESENT);
intentFilter.priority = 999
registerReceiver(mReceiver, intentFilter)
super.onCreate()
Log.d("LockService", "onCreate()")
}
override fun onDestroy() {
if (isShowing) mWindowManager!!.removeView(mView)
if (mThread != null && mThread!!.isAlive) mThread!!.interrupt()
// wl.release()
unregisterReceiver(mReceiver)
super.onDestroy()
Log.d("LockService", "onDestroy()")
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.d("LockService", "onStartCommand()")
return START_STICKY
}
}
Ответы (1 шт):
Автор решения: SurfaceStack
→ Ссылка
Посмотрите в сторону самодельного экрана блокировки. Например здесь, (надо в MyService.java добавить проверку на время в методе onCreate() после filter.addAction(...)).