Home > Articles > Mobile Application Development & Programming

  • Print
  • + Share This
This chapter is from the book

Sending KeyEvents to the TV

We’ll continue to follow the basic structure of a remote app. The Anymote protocol can handle sending Android Intents, KeyEvents, and mouse events. Using KeyEvents, an app can send whatever keystrokes are entered on a remote device directly to the Google TV app that is being displayed. It can be used to enter search terms on YouTube or other apps, and it is a way to fill out a web form being displayed in the Google Chrome browser.

Handling onKeyDown and onKeyUp Methods

By implementing an Activity’s onKeyDown and onKeyUp methods for the Activity, we can correctly send KeyEvents to the TV using the Anymote protocol. We’ll do that in an Activity called Hour23EchoRemote. Listing 23.6 shows the onKeyDown and onKeyUp methods.

Listing 23.6. onKeyDown and onKeyUp Methods

1:  @Override
2:    public boolean onKeyDown(int keyCode, KeyEvent event) {
3:      anymoteSender.sendKey(Code.valueOf(keyCode),Key.Action.DOWN);
4:      return true;
5:    }
6:    @Override
7:      public boolean
onKeyUp(int keyCode, KeyEvent event) {
8:        anymoteSender.sendKey(Code.valueOf(keyCode),Key.Action.UP);
9:        return true;
10:   }

Listing 23.6 used the anymoteSender.sendKey method in lines 3 and 8. The sendKey method takes a Code and an Action as parameters. The keyCode parameter passed to the onKeyUp and onKeyDown events is an int. That int corresponds to the physical key pressed. The sendKey method requires a Code corresponding to that key. The Code class was defined in the keycodes.proto file. By using Code.valueOf(keyCode) in line 8, we get the desired result.

The Action represents either a key up or key down event.

By explicitly using these KeyEvents, we are sending the TV precisely what was entered by pressing a key on the remote device. If the user presses the special keys on the phone to shift to capital letters, numbers, or special characters, that is reflected in what is sent to the TV.

The Blackjack app used a different method. When the user chose Hit or Stand, the H or S key was sent using the anymoteSender.sendKeyPress method:


Where keyEvent is either KeyEvent.KEYCODE_H or KeyEvent.KEYCODE_S. The sendKeyPress method always sends the value of the physical key from the keyboard. It will send a capital H regardless of whether lowercase h or uppercase H was selected on the device.

For this example app, we implemented the onKeyUp and onKeyDown methods for the Activity itself. There was no UI with EditText fields to accept the input. If there had been, the Activity would never have received the keystrokes. The EditText would have consumed them.

To show the keyboard on the Activity, we use the following setting in the Android Manifest for this project:


The method setOnKeyListener (View.OnKeyListener l) is available for all Views. An alternative to showing an empty Activity with an onscreen keyboard for input is to create a UI that uses Views like EditText fields to send keystrokes to the TV. To do that, the EditText field would use the setOnKeyListener method.

The onKeyListener requires the onKey method to be implemented. That method passes the keyCode and KeyEvent, so it would be straightforward to implement an input field for accepting keystrokes and sending them to the TV. The definition of the onKey method looks like this:

public abstract boolean onKey (View v, int keyCode, KeyEvent event)

Developing the TV App

We can use the Hour23EchoRemote app as the basis for creating an interactive app. We defined the remote app. Now we need to create the companion app that runs on the TV. Our remote app just sends keystrokes to the TV, so our companion app on the TV will be very simple. We’ll show an EditText field that can be filled out.

Listing 23.7 shows the entire app that runs on the TV. The layout file contains an EditText field to display input. When a key is pressed in the remote app, it is sent to the companion app and displayed. Uppercase, lowercase, and special characters are sent properly from the remote to the TV. To tie these apps together, we must modify the remote code to launch this app on the TV when pairing is complete.

Listing 23.7. Companion Activity on TV for Echoing Keystrokes

1:  public class Hour23Echo extends Activity {
2:    EditText echoText;
3:    @Override
4:      public void onCreate(Bundle savedInstanceState) {
5:        super.onCreate(savedInstanceState);
6:        setContentView(R.layout.main);
7:        echoText= (EditText)findViewById(R.id.echo);
8:    }
9:  }

Our goal is for the companion app to launch on the TV when the remote app successfully pairs with the TV. We can look to the Blackjack app as a model for how to do this. We need to fling the Intent we want to start on the TV. That is, we want to create an Intent that corresponds to the Activity on the TV and start that Activity.

Listing 23.8 shows the code to start the TV app from the remote app. The action occurs in the onConnected method.

The name of the Activity on the TV is Hour23Echo, but more specifically, we should refer to the package name and the class name to create the intent. The package name is com.bffmedia.hour23echo. In line 5 of Listing 23.8, we create a new Intent named echoIntent. Lines 6 to 8 set echoIntent to the values for the package and the class. Line 9 sends the Intent from the remote app to the TV. We are creating Android Intent and using the Anymote protocol to request that the TV launch the Intent.

Listing 23.8. Starting the Companion App from the Remote App

1:  @Override
2:  public void onConnected(final AnymoteSender anymoteSender) {
3:    this.anymoteSender = anymoteSender;
4:    //Add this section to open the Companion App
5:    final Intent echoIntent = new Intent("android.intent.action.MAIN");
6:    echoIntent.setComponent(new ComponentName(
7:        "com.bffmedia.hour23echo",
8:        "com.bffmedia.hour23echo.Hour23Echo"));
9:    anymoteSender.sendIntent(echoIntent);
10:   handler.post(new Runnable() {
11:     public void run() {
12:       progressBar.setVisibility(View.INVISIBLE);
13:     }
14:   });
15: }
  • + Share This
  • 🔖 Save To Your Account