• 双进程守护


    定义一个本地服务,在该服务中播放无声音乐,并绑定远程服务。

    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) {
    }

    //隐藏服务通知
    try {
    if (Build.VERSION.SDK_INT < 25) {
    startService(Intent(this, HideForegroundService::class.java))
    }
    } catch (e: Exception) {
    }

    if (KeepLive.keepLiveService != null) {
    KeepLive.keepLiveService!!.onWorking()
    }
    return Service.START_STICKY
    }

    private fun play() {
    if (mediaPlayer != null &amp;&amp; !mediaPlayer!!.isPlaying) {
    mediaPlayer?.start()
    }
    }

    private inner class MyBilder : GuardAidl.Stub() {

    @Throws(RemoteException::class)
    override fun wakeUp(title: String, discription: String, iconRes: Int) {

    }
    }

    private val connection = object : ServiceConnection {

    override fun onServiceDisconnected(name: ComponentName) {
    val remoteService = Intent(this@LocalService,
    RemoteService::class.java)
    this@LocalService.startService(remoteService)
    val intent = Intent(this@LocalService, RemoteService::class.java)
    this@LocalService.bindService(intent, this,
    Context.BIND_ABOVE_CLIENT)
    }

    override fun onServiceConnected(name: ComponentName, service: IBinder) {
    try {
    if (mBilder != null &amp;&amp; KeepLive.foregroundNotification != null) {
    val guardAidl = GuardAidl.Stub.asInterface(service)
    guardAidl.wakeUp(KeepLive.foregroundNotification?.getTitle(), KeepLive.foregroundNotification?.getDescription(), KeepLive.foregroundNotification!!.getIconRes())
    }
    } catch (e: RemoteException) {
    e.printStackTrace()
    }

    }
    }

    override fun onDestroy() {
    super.onDestroy()
    unbindService(connection)
    if (KeepLive.keepLiveService != null) {
    KeepLive.keepLiveService?.onStop()
    }
    }
    }

  • 相关阅读:
    Silverlight 2中实现文件上传和电子邮件发送
    Silverlight结合Web Service进行文件上传
    silverlight DataGrid 内嵌ComboBox 实现加载和保存
    silverlight 使用IValueConverter 转换
    检测场所条件查询
    代码中的坏味道
    Prism初研究之Bootstrapper
    Prism初研究之简介
    编写可读代码的艺术
    ffmpeg怎么样处理网络流
  • 原文地址:https://www.cnblogs.com/hyhy904/p/11291157.html
Copyright © 2020-2023  润新知