Android Question Start a service or app pressing a button from the device.

George Anifantakis

Member
Licensed User
Longtime User
Hi, i need to start a service or activity by pressing a button from a Bluetooth remote device from home screen or when the device is idle.

Below the logs when pressing the button.

ViewRoot's KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ENTER, scanCode=28, metaState=0, flags=0x8, repeatCount=0, eventTime=33143014, downTime=33143014, deviceId=29, source=0x101 } to com.android.keyguard.KeyguardViewManager$ViewManagerHost{4211a9f8 V.E..... ........ 0,0-720,1280}
interceptKeyTi keyCode=66 down=false repeatCount=0 keyguardOn=true mHomePressed=false canceled=false
ViewRoot's KeyEvent { action=ACTION_UP, keyCode=KEYCODE_ENTER, scanCode=28, metaState=0, flags=0x8, repeatCount=0, eventTime=33143017, downTime=33143014, deviceId=29, source=0x101 } to com.android.keyguard.KeyguardViewManager$ViewManagerHost{4211a9f8 V.E..... ........ 0,0-720,1280}

It sends KEYCODE_ENTER. I've tried using a static receiver with the below

AddReceiverText(s1,
<intent-filter android:priority="2147483647">
<action android:name="android.intent.action.KEYCODE_ENTER"/>
</intent-filter>
)

but the service didn't started. Perhaps the event "android.intent.action.KEYCODE_ENTER" is not correct. Any ideas how to trap this?
 
Last edited:

George Anifantakis

Member
Licensed User
Longtime User
android.intent.action.MEDIA_BUTTON does not trigger the service. If it helps, i trap the keycode in activity and the reference number is 66.
 
Upvote 0

George Anifantakis

Member
Licensed User
Longtime User
I managed to activate it using the other button of the remote shutter that sends VOLUME_CHANGED event but it doesn't work when the phone is idle, screen off. Is there any workaround?
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
If it doesn't work when the phone is IDLE this is what Erel suggested : the activity is receiving the event when it is in the foreground (normal behavior).
If you want to be able to receive the event even when the activity is not in the foreground, you would have to register an intent receiver as Erel suggested above. We have tried to work on such intent previously (https://www.b4x.com/android/forum/threads/android-intent-action-media_button-again.48890/). Perhaps could this help.
And about registering an intent receiver in the Manifest file (https://www.b4x.com/android/forum/t...sms-messages-in-the-background.20103/#content).

Edit : something about volume press and services https://www.b4x.com/android/forum/threads/intercept-volume-button-press.28179/#post-163378
 
Upvote 0

George Anifantakis

Member
Licensed User
Longtime User
I've tried with BroadcastReceiver library as well. The behavior is the same as using a static receiver in the manifest file. The event works only if the phone is active no matter if the application is in the foreground or in the background. If the device is idle screen off or locked if doesn't work at all....
I could not been able to find any workaround so far..

Thanks a lot for your help.
 
Upvote 0
Top