I'm asking for text input on Wear OS using the RemoteInput intent, with the following code:
public class MainActivity extends Activity {
private ActivityMainBinding binding;
private static final int MESSAGE_INPUT_RESULT = 102;
private static final String RESULT_KEY = "input_result";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
binding.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Prepare the input request
Intent remoteInputIntent = new Intent("android.support.wearable.input.action.REMOTE_INPUT");
RemoteInput[] remoteInputArray = new RemoteInput[1];
remoteInputArray[0] = new RemoteInput.Builder(RESULT_KEY)
.setAllowFreeFormInput(true)
.setLabel("Your message to Bob")
.setChoices(new CharSequence[] {"OK", "Thanks"})
.build();
// TODO: this DOESN'T WORK, I need to pass an additional value
int myValue = 55;
remoteInputIntent.putExtra("myValue", myValue);
// TODO:
// Display the input request
remoteInputIntent.putExtra("android.support.wearable.input.extra.REMOTE_INPUTS", remoteInputArray);
startActivityForResult(remoteInputIntent, MESSAGE_INPUT_RESULT);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MESSAGE_INPUT_RESULT && resultCode == RESULT_OK) {
Bundle bundle = RemoteInput.getResultsFromIntent(data);
if (bundle != null) {
// TODO: NONE of the two statements below work!
int myValue = data.getIntExtra("myValue", 0);
//int myValue = bundle.getInt("myValue");
// Get the text the user typed
CharSequence charSequence = bundle.getCharSequence(RESULT_KEY);
if (charSequence != null && charSequence.length() > 0)
// Display the text and the value in a toast
Toast.makeText(this, charSequence + ", myValue = " + String.valueOf(myValue), Toast.LENGTH_SHORT).show();
}
}
}
}
However, in the toast that is displayed, myValue
is always 0
. How can I pass additional data using the same intent, so that I will be able to read it in the onActivityResult
routine and take appropriate actions?