How to simulate home, back, menu, search, task and volume buttons?
Asked Answered
H

7

6

I want to simulate all physical buttons of our Android devices.

So is there a way to simulate:

  • BACK BUTTON
  • HOME BUTTON
  • MENU BUTTON
  • SEARCH BUTTON
  • TASK BUTTON
  • VOLUME (+ AND -) BUTTONS
Hashum answered 14/12, 2012 at 9:54 Comment(4)
You want access those buttons from java code?Perjured
there are lots of solution on GOOGLE..Aciniform
@RajeshRajaram No I want to go to home or show menu (etc etc) programmaticallyHashum
@AnandTiwari I found only Home Button code https://mcmap.net/q/479083/-android-simulate-home-clickHashum
R
17

Create a KeyEvent and publish it.

KeyEvent kdown = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK);
Activity.dispatchKeyEvent(kdown);
KeyEvent kup = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK);
Activity.dispatchKeyEvent(kup);
Rojo answered 14/12, 2012 at 9:56 Comment(5)
Many thanks..but I want to do this programmatically and not only from an activity...Hashum
This is exactly "programmatically" and you can always have a reference of your activity to call this method (you are not forced to call it within your activity).Poultryman
@Aleksander Gralak : Your code does not call the activity.onBackPressed method btw...Poultryman
You need to call it for both action down and action up. Like so this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK)); this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));Promulgate
this method doesn't work anymore: ``` ComponentActivity.dispatchKeyEvent can only be called from within the same library group prefix(referenced groupId=androidx.core)```Aubreir
M
3

You can simulate pressing buttons even your app is closed, but you'll need Accessibility permission:

Create service extended from AccessibilityService:

class ExampleAccessService:AccessibilityService() {
    override fun onInterrupt() {
    }

    override fun onAccessibilityEvent(event: AccessibilityEvent?) {
    }
    
    fun doAction(){
        performGlobalAction(GLOBAL_ACTION_RECENTS)
//        performGlobalAction(GLOBAL_ACTION_BACK)
//        performGlobalAction(GLOBAL_ACTION_HOME)
//        performGlobalAction(GLOBAL_ACTION_NOTIFICATIONS)
//        performGlobalAction(GLOBAL_ACTION_POWER_DIALOG)
//        performGlobalAction(GLOBAL_ACTION_QUICK_SETTINGS)
//        performGlobalAction(GLOBAL_ACTION_TOGGLE_SPLIT_SCREEN)
    }
}

Call doAction() where you want action

Add to Manifest:

<application
...
    <service
        android:name=".ExampleAccessService"
        android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
        android:label="Name of servise" // it will be viewed in Settings->Accessibility->Services
        android:enabled="true"
        android:exported="false" >
        <intent-filter>
            <action android:name="android.accessibilityservice.AccessibilityService"/>
        </intent-filter>
        <meta-data
            android:name="android.accessibilityservice"
            android:resource="@xml/accessibility_service_config"/>
    </service>
...
</application>

accessibility_service_config.xml:

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeAllMask"
    android:accessibilityFeedbackType="feedbackAllMask"
    android:accessibilityFlags="flagDefault"
    android:canRetrieveWindowContent="false"
    android:description="your description"
    android:notificationTimeout="100"
    android:packageNames="your app package, ex: ex: com.example.android"
    android:settingsActivity="your settings activity ex: com.example.android.MainActivity" />

for more info look at https://developer.android.com/guide/topics/ui/accessibility/services.html

Mielke answered 1/2, 2018 at 15:59 Comment(0)
G
0

If you test your application's specific component such as Activity so you may use InstrumentationTestCase's sendKeys()method, passing there any combination of keys. Also you can use TouchUtils to simulate tapping, dragging and clicking actions

Gibran answered 14/12, 2012 at 9:58 Comment(0)
N
0

Try using android dispatchKeyEvent override method.

Nearly answered 13/7, 2020 at 2:14 Comment(0)
Y
-1

Use onKeyListener for overriding the physical buttons in your device. See the doc.

The KeyEvent class have all the values for the physical buttons and on screen keyboard on your device. For Example KeyEvent.KEYCODE_HOME substitutes for the home button in the device.

Google it, you can find a lot of examples for KEYCODE events

Yield answered 14/12, 2012 at 10:7 Comment(0)
A
-1

Simulation for following keys:

-BACK BUTTON : override onBackPressed() in current activity.

-MENU BUTTON : you should look here and also openOptionsMenu(), menu

you should also look at this blog.

Aciniform answered 15/12, 2012 at 5:17 Comment(2)
back button doesn't always close the current activity, especially if the activity has "onBackPressed" being implemented in a different way.Nickola
Also this won't work if you want to simulate the button click as part of a unit test.Marou
P
-2

Try this your activity

public boolean onKeyDown(int keyCode, KeyEvent event)   
{
 if (keyCode == KeyEvent.KEYCODE_BACK)
 {

 }
 if (keyCode == KeyEvent.KEYCODE_HOME)
 {

 }
 if (keyCode == KeyEvent.KEYCODE_MENU)
 {

 }
 if (keyCode == KeyEvent.KEYCODE_VOLUME_UP)
 {

 }
 if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
 {

 }
}
Perjured answered 14/12, 2012 at 10:16 Comment(2)
Many thanks but I don't want to pick buttons event but to simulate themHashum
wrong direction.. the question clearly states simulate, not catchGlochidiate

© 2022 - 2024 — McMap. All rights reserved.