最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - Need to Detect Volume Btns when the screen is off - Stack Overflow

programmeradmin5浏览0评论

I am working on an application that send command to my python server on my laptop when volume btns pressed like this

const volumeUpSubscription = eventEmitter.addListener(
      "onVolumeUp",
      async() => {
        console.log("Volume Uppppp pressed!");
        sendCommand("NEXT_SLIDE")
      }
    );

    // Similar changes for volumeDownSubscription
  const volumeDownSubscription = eventEmitter.addListener(
    "onVolumeDown",
    async() => {
      console.log("Volume Down pressed!");
      sendCommand("PREV_SLIDE")
    }
  );

I am using Native Module only for android rightnow like this:

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
    super.onKeyDown(keyCode, event)
    Log.d("MainActivity", "onKeyDown called with keyCode: $keyCode")
    VolumeModule.instence?.onKeyDownEvent(keyCode)
    return true
}

override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
    super.onKeyUp(keyCode, event)
    Log.d("MainActivity", "onKeyUp called with keyCode: $keyCode")
    return true
}

and here is my Native Module Class:

class VolumeModule(private val reactContext: ReactApplicationContext) :
ReactContextBaseJavaModule(reactContext), LifecycleEventListener {

private val TAG = "VolumeManagerModule"
private var isListening = false

init {
    reactContext.addLifecycleEventListener(this)
}

override fun getName(): String {
    return "VolumeManager"
}

// This method sends events to JavaScript
private fun sendEventToJS(eventName: String, params: Any?) {
    reactContext
        .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
        .emit(eventName, params)
}

@ReactMethod
fun startListening() {
    Log.d(TAG, "startListening called")
    if (!isListening) {
        sendEventToJS("onListeningStarted", null)
        isListening = true
    }
}

@ReactMethod
fun stopListening() {
    Log.d(TAG, "stopListening called")
    isListening = false
}

//This funtion will be called when a key is pressed
fun onKeyDownEvent(keyCode: Int) {
    Log.d("nKeyDownEvent", "Called inside nKeyDownEvent $keyCode")
    if (true) {
        when (keyCode) {
            KeyEvent.KEYCODE_VOLUME_UP -> {
                Log.d(TAG, "Volume Up pressed")
                sendEventToJS("onVolumeUp", null)
            }
            KeyEvent.KEYCODE_VOLUME_DOWN -> {
                Log.d(TAG, "Volume Down pressed")
                sendEventToJS("onVolumeDown", null)
            }
            else -> {
                Log.d(TAG, "Other Key pressed: $keyCode")
            }
        }
    }
}

override fun onHostResume() {
    Log.d(TAG, "onHostResume called")
    startListening()
}

override fun onHostPause() {
    Log.d(TAG, "onHostPause called")
    stopListening()
}

override fun onHostDestroy() {
    Log.d(TAG, "onHostDestroy called")
    stopListening()
}

companion object {
    var instence: VolumeModule? = null
        private set

    fun initModuleInstence(reactContext: ReactApplicationContext): VolumeModule? {
        instence = VolumeModule(reactContext)
        return instence
    }
}

}

SO my point is the app is working perfectly but How can i Implement that when the screen is off and the app is able to detect the Volume Btns and send commands

I am working on an application that send command to my python server on my laptop when volume btns pressed like this

const volumeUpSubscription = eventEmitter.addListener(
      "onVolumeUp",
      async() => {
        console.log("Volume Uppppp pressed!");
        sendCommand("NEXT_SLIDE")
      }
    );

    // Similar changes for volumeDownSubscription
  const volumeDownSubscription = eventEmitter.addListener(
    "onVolumeDown",
    async() => {
      console.log("Volume Down pressed!");
      sendCommand("PREV_SLIDE")
    }
  );

I am using Native Module only for android rightnow like this:

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
    super.onKeyDown(keyCode, event)
    Log.d("MainActivity", "onKeyDown called with keyCode: $keyCode")
    VolumeModule.instence?.onKeyDownEvent(keyCode)
    return true
}

override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
    super.onKeyUp(keyCode, event)
    Log.d("MainActivity", "onKeyUp called with keyCode: $keyCode")
    return true
}

and here is my Native Module Class:

class VolumeModule(private val reactContext: ReactApplicationContext) :
ReactContextBaseJavaModule(reactContext), LifecycleEventListener {

private val TAG = "VolumeManagerModule"
private var isListening = false

init {
    reactContext.addLifecycleEventListener(this)
}

override fun getName(): String {
    return "VolumeManager"
}

// This method sends events to JavaScript
private fun sendEventToJS(eventName: String, params: Any?) {
    reactContext
        .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
        .emit(eventName, params)
}

@ReactMethod
fun startListening() {
    Log.d(TAG, "startListening called")
    if (!isListening) {
        sendEventToJS("onListeningStarted", null)
        isListening = true
    }
}

@ReactMethod
fun stopListening() {
    Log.d(TAG, "stopListening called")
    isListening = false
}

//This funtion will be called when a key is pressed
fun onKeyDownEvent(keyCode: Int) {
    Log.d("nKeyDownEvent", "Called inside nKeyDownEvent $keyCode")
    if (true) {
        when (keyCode) {
            KeyEvent.KEYCODE_VOLUME_UP -> {
                Log.d(TAG, "Volume Up pressed")
                sendEventToJS("onVolumeUp", null)
            }
            KeyEvent.KEYCODE_VOLUME_DOWN -> {
                Log.d(TAG, "Volume Down pressed")
                sendEventToJS("onVolumeDown", null)
            }
            else -> {
                Log.d(TAG, "Other Key pressed: $keyCode")
            }
        }
    }
}

override fun onHostResume() {
    Log.d(TAG, "onHostResume called")
    startListening()
}

override fun onHostPause() {
    Log.d(TAG, "onHostPause called")
    stopListening()
}

override fun onHostDestroy() {
    Log.d(TAG, "onHostDestroy called")
    stopListening()
}

companion object {
    var instence: VolumeModule? = null
        private set

    fun initModuleInstence(reactContext: ReactApplicationContext): VolumeModule? {
        instence = VolumeModule(reactContext)
        return instence
    }
}

}

SO my point is the app is working perfectly but How can i Implement that when the screen is off and the app is able to detect the Volume Btns and send commands

Share Improve this question asked Mar 23 at 23:03 XeroXero 1 1
  • 1 Not clear if you are aware of Doze mode which will prevent your app from running in order to save battery life. – Morrison Chang Commented Mar 24 at 2:07
Add a comment  | 

1 Answer 1

Reset to default 1

You can try using a CoroutineScope, on the io thread (remember that io does not handle the interface so you will have to use a with context on the Main thread to paint something on it).

发布评论

评论列表(0)

  1. 暂无评论