Mr.*_*tel 6 service android overlay lockscreen android-8.0-oreo
在我的应用程序中,当我运行服务时,我尝试在此处覆盖服务的布局,当目标设备低于oreo时,该服务成功执行并在锁屏上显示该布局,但是当我尝试从版本覆盖该布局时比奥利奥晚,它不起作用,但是当我按照其他答案中的建议将标志更改为Type_Application_Overlay时,它仅在屏幕解锁时显示布局,但我想在锁定屏幕上显示它,我尝试了很多搜索,但未找到任何有用的答案解决这个问题的一些答案建议我在锁屏上显示一个活动,我也尝试过,但仍然没有任何帮助!
此外,要添加在Play商店中的一些应用,这些应用甚至可以在oreo和pie上轻松显示锁定屏幕上的任何布局:
您可以看一下这个应用程序:
https://play.google.com/store/apps/details?id=com.wifihacker.whousemywifi.wifirouter.wifisecurity
这个应用程序可以轻松地在锁屏上显示自定义的全屏布局,即使不要求任何叠加权限或设备管理员权限也是如此。
因此,如果此应用程序能够覆盖锁定屏幕,为什么我不能做同样的事情?
对此的任何帮助和建议将不胜感激!
这是我当前的锁屏服务代码:
class LockScreenService : Service() {
private lateinit var mReceiver: BroadcastReceiver
private var isShowing = false
private lateinit var windowManager: WindowManager
lateinit var params: WindowManager.LayoutParams
lateinit var myview: View
var downX:Int = 0
lateinit var locklayout:RelativeLayout
var upX:Int = 0
var indicator: WaveLoadingView?=null
lateinit var date:TextView
lateinit var time:TextView
lateinit var settings:ImageView
lateinit var unlock:LinearLayout
var r:Runnable?=null
companion object{
}
private val mBatInfoReceiver = object : BroadcastReceiver() {
override fun onReceive(ctxt: Context, intent: Intent) {
val level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)
if (indicator!=null)
{
indicator!!.progressValue = level
indicator!!.setAnimDuration(3000)
indicator!!.startAnimation()
}
}
}
override fun onBind(intent: Intent): IBinder? {
// TODO Auto-generated method stub
return null
}
override fun onCreate() {
super.onCreate()
try {
EventBus.getDefault().register(this)
windowManager = applicationContext.getSystemService(WINDOW_SERVICE) as WindowManager
val li = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
myview = li.inflate(R.layout.lockscreenlayout, null)
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
params = WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT
)
} else {
params = WindowManager.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
or WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
or WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
or WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
PixelFormat.TRANSLUCENT
)
myview.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_FULLSCREEN
or View.SYSTEM_UI_FLAG_VISIBLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_IMMERSIVE
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
)
}
myview.setVisibility(View.VISIBLE)
settings = myview.findViewById(R.id.imgsetting)
settings.setOnClickListener {
var intent = Intent(this, Settings::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
this.startActivity(intent)
windowManager.removeViewImmediate(myview)
}
indicator = myview.findViewById(R.id.indicator)
locklayout = myview.findViewById(R.id.locklay)
this.registerReceiver(this.mBatInfoReceiver, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
time = myview.findViewById(R.id.time)
date = myview.findViewById(R.id.date)
date.text =
SimpleDateFormat("EEEE").format(Calendar.getInstance().time).toString() + "," + SimpleDateFormat("dd").format(
Calendar.getInstance().time
).toString() + " " + SimpleDateFormat("MMMM").format(Calendar.getInstance().time).toString()
try {
unlock.setOnTouchListener(object : View.OnTouchListener {
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
if (event!!.getAction() == MotionEvent.ACTION_DOWN) {
downX = event.getX().toInt()
return true
} else if (event.getAction() == MotionEvent.ACTION_UP) {
upX = event.getX().toInt()
if (upX - downX > 100) {
val animation = AnimationUtils.loadAnimation(applicationContext, R.anim.left_right_anim)
animation.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationStart(animation: Animation) {}
override fun onAnimationRepeat(animation: Animation) {}
override fun onAnimationEnd(animation: Animation) {
windowManager.removeViewImmediate(myview)
}
})
locklayout.startAnimation(animation)
// swipe right
} else if (downX - upX > -100) {
}
return true
}
return false
}
})
} catch (ex: Exception)
{}
//Register receiver for determining screen off and if user is present
mReceiver = LockScreenStateReceiver()
val filter = IntentFilter(Intent.ACTION_SCREEN_OFF)
filter.addAction(Intent.ACTION_USER_PRESENT)
registerReceiver(mReceiver, filter)
}
catch (ex:Exception)
{
}
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
return START_STICKY
}
inner class LockScreenStateReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
try {
if (intent.action == Intent.ACTION_SCREEN_OFF) {
//if screen is turn off show the textview
if (!isShowing) {
windowManager.addView(myview, params)
isShowing = true
}
} else if (intent.action == Intent.ACTION_USER_PRESENT) {
//Handle resuming events if user is present/screen is unlocked remove the textview immediately
if (isShowing) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {
//windowManager.removeViewImmediate(myview)
}
isShowing = false
}
}
}
catch (ex:Exception){
}
}
}
override fun onDestroy() {
//unregister receiver when the service is destroy
try {
EventBus.getDefault().unregister(this)
if (mReceiver != null) {
unregisterReceiver(mReceiver)
}
//remove view if it is showing and the service is destroy
if (isShowing) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {
//windowManager.removeViewImmediate(myview)
}
isShowing = false
}
}
catch (ex:Exception)
{
}
super.onDestroy()
}
}
Run Code Online (Sandbox Code Playgroud)
我非常感谢@Emir的回答,但是他提供的代码未在某些设备中运行,并且活动未在锁定屏幕上启动
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Run Code Online (Sandbox Code Playgroud)
这里的问题是代码的“ OR”部分导致某些设备出现问题,因此要解决此问题,而不是在“ OR”部分中调用窗口标志,而我单独调用了所有这些标志,这是我在oncreate中所做的解决这个问题。
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_lock_screen)
//execute all flags individually to solve this issue
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED)
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD)
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON)
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
val mUIFlag = ( View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_FULLSCREEN
or View.SYSTEM_UI_FLAG_VISIBLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_IMMERSIVE
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
window.decorView.systemUiVisibility = mUIFlag
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
481 次 |
| 最近记录: |