在我的应用程序中,我正在尝试使用服务叠加布局,当我运行服务时,它可以成功执行并在锁屏上显示布局,但目标设备版本低于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()
}
}