unregisterReceiver(br)
} catch (e: IllegalArgumentException) {
}
super.onDestroy()
}
/**
- 检查屏幕是否点亮
*/
private fun checkScreenOn() {
val pm = this@OnePixelActivity.getSystemService(Context.POWER_SERVICE) as PowerManager
val isScreenOn = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
pm.isInteractive
} else {
pm.isScreenOn
}
if (isScreenOn) {
finish()
}
}
}
#####2.双进程守护
定义一个本地服务,在该服务中播放无声音乐,并绑定远程服务。
class LocalService : Service() {
private var mediaPlayer: MediaPlayer? = null
private var mBilder: MyBilder? = null
override fun onCreate() {
super.onCreate()
if (mBilder == null) {
mBilder = MyBilder()
}
}
override fun onBind(intent: Intent): IBinder? {
return mBilder
}
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
//播放无声音乐
if (mediaPlayer == null) {
mediaPlayer = MediaPlayer.create(this, R.raw.novioce)
//声音设置为0
mediaPlayer?.setVolume(0f, 0f)
mediaPlayer?.isLooping = true//循环播放
play()
}
//启用前台服务,提升优先级
if (KeepLive.foregroundNotification != null) {
val intent2 = Intent(applicationContext, NotificationClickReceiver::class.java)
intent2.action = NotificationClickReceiver.CLICK_NOTIFICATION
val notification = NotificationUtils.createNotification(this, KeepLive.foregroundNotification!!.getTitle(), KeepLive.foregroundNotification!!.getDescription(), KeepLive.foregroundNotification!!.getIconRes(), intent2)
startForeground(13691, notification)
}
//绑定守护进程
try {
val intent3 = Intent(this, RemoteService::class.java)
this.bindService(intent3, connection, Context.BIND_ABOVE_CLIENT)
} catch (e: Exception) {
}
//隐藏服务通知<