Development
Step 1: Implementation of Remote Actions
-
Check for the activity to handle the KeyEvent in a manifest file,
AndroidManifest.xml
. -
Add
<intent-filter>
and<meta-data>
elements to that activity.
AndroidManifest.xml
resource file definingRemoteActions
must be specified for<meta-data>
.<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.spenremote.racingcar"> <application ... <activity android:name=".MainActivity" <intent-filter> <action android:name="com.samsung.android.support.REMOTE_ACTION" /> </intent-filter> <meta-data android:name="com.samsung.android.support.REMOTE_ACTION" android:resource="@xml/remote_action_sample"/> </activity> </application> </manifest>
Note :Only one
RemoteAction
per app is allowed at the moment. If you defineRemoteActions
to several activities, all other except one may be ignored.
Tip :Learn more by watching the video
-
Create an
.xml
file under res/xml/. (Name the file with the same name as the resource specified previously) -
Create the desired
<action>
elements in the XML file created as seen below. XML has a root element of<remote-actions>
, and may include several<action>
elements. In addition, each<action>
contains information about id, label, priority, trigger_key, etc.
If you want to map a default action to a specific gesture, you need to add a<preference>
element.<?xml version="1.0" encoding="utf-8"?> <remote-actions version="1.2"> <action id="pause_or_resume" label="@string/pause_or_resume" priority="1" trigger_key="SPACE"> <preference name="gesture" value="click"/> <preference name="button_only" value="true"/> </action> <action id="move_left" label="@string/move_car_left" priority="2" trigger_key="DPAD_LEFT"> <preference name="gesture" value="swipe_left"/> <preference name="motion_only" value="true"/> </action> <action id="move_right" label="@string/move_car_right" priority="3" trigger_key="DPAD_RIGHT"> <preference name="gesture" value="swipe_right"/> <preference name="motion_only" value="true"/> </action> <action id="restart" label="@string/restart" priority="4" trigger_key="CTRL+R"> <preference name="gesture" value="circle_cw|circle_ccw"/> <preference name="motion_only" value="true"/> </action> </remote-actions>
Tip :Learn more by watching the video
Step 2: Implementation of KeyEvent-Callback
Apply the KeyEvent-Callback to the activity where RemoteActions
have been declared.
Please refer to the Android Developers website for detailed guidelines. It is recommended to handle the sent KeyEvent at onKeyDown
.
Android Developers Website: https://developer.android.com/reference/android/view/KeyEvent.Callback.html
Add the following KeyEvent-Callback as seen below:
- Space key: Pause or Resume the racing game
- Left key: Move the car to left direction
- Right key: Move the car to right direction
- Ctrl + R key: Restart the racing game
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SPACE) {
RacingCar.PlayState state = racingCar.getPlayState();
if (state == RacingCar.PlayState.Playing) {
pause();
} else {
resume();
}
} else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
moveCarTo(RacingCar.POS_LEFT);
} else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
moveCarTo(RacingCar.POS_RIGHT);
} else if ((event.getMetaState() & KeyEvent.META_CTRL_ON) != 0
&& (keyCode == KeyEvent.KEYCODE_R)) {
restart();
}
return super.onKeyDown(keyCode, event);
}
Pay attention to cases when the child view consumes the KeyEvent
. For example, if the EditText
is focused on a screen where the EditText
co-exists, most KeyEvents
will be consumed for text inputs or a cursor movement and they may not be sent to the Activities or Container Layouts. On this screen, you must specify the key code or key combination that the child view cannot consume.
Learn more by watching the video
Now everything is ready. When you have installed your app, make sure that your app icon appears properly in the settings below. Settings > Advanced features > S Pen > Air actions
You're done!
Congratulations! You have successfully achieved the goal of this Code Lab activity. Now, you can map air actions events on a game or app by yourself! But, if you're having trouble, you may check out the link below.