应用场景:在service开启的时候,回执一个状态,通过判断状态值,确定是否弹出提示框
在service子类中的代码:
if (demo == "code not match") {
Log.i(tag, demo.toString()+" true")
runOnUiThread {
val alert = AlertDialog.Builder(applicationContext).apply {
setTitle(R.string.hint)
setMessage(R.string.Device_mismatch)
setCancelable(false)
setPositiveButton(R.string.ok) { dialogInterface: DialogInterface, i: Int ->
//finish()
}
}
Device_mismatch=alert.create()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//8.0新特性
Device_mismatch!!.window!!.setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY - 1);
} else {
Device_mismatch!!.window!!.setType(WindowManager.LayoutParams.TYPE_TOAST);
}
Device_mismatch!!.show()
}
}
参考文件为:链接