bill smith
bill smith

Reputation: 31

how to call activity from inputmethodservice and get response

I have a soft keyboard as a inputmethodservice calling an activity. A button is pressed and a scanner is activated. The scanner activity captures a set of data then returns the data to the inputmethodservice for filling in a text field on a browser.

How do I return the scan value of the activity back to the inputmethodservice?

Thanks for any help.

Upvotes: 3

Views: 3722

Answers (3)

Lucas
Lucas

Reputation: 1246

Here is a code to test the solution using Dialog:

// 1. CREATE THE DIALOG
val builder: AlertDialog.Builder = AlertDialog.Builder(this, R.style.Theme_AppCompat_Light)
builder.setTitle("Title").setMessage("This is the message for the user. ")
val mDialog = builder.create()

// 2. SET THE IME WINDOW TOKEN ATTRIBUTE WITH THE TOKEN OF THE KEYBOARD VIEW 
mDialog.window?.attributes?.token = this.mKeyboardView.windowToken

// 3. SET THE TYPE OF THE DIALOG TO TYPE_APPLICATION_ATTACHED_DIALOG
mDialog.window?.setType(WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG)

// 4. SHOW THE DIALOG 
mDialog.show()

Upvotes: 2

hackbod
hackbod

Reputation: 91331

You generally can't do this from a Service. Because a Service is not an Activity, it can not receive results from an Activity, and thus does not have startActivityForResult().

In fact you probably don't want to do this. Starting an activity is at best going to be a clunky experience -- a user doing input into a field doesn't really want to context switch out to another application's activity as part of their input.

Worse, this opens the door to a lot of ugly interactions. For example, if they press home or use a notification or recent apps to switch to another application, what will happen to your activity? Probably nothing good.

Instead I would recommend just showing a dialog as part of the IME -- use the Dialog class and set its window type to TYPE_INPUT_METHOD_DIALOG. This will display on top of your IME, allowing you to interact with the user without disrupting the editing session. For example, you'll want to set this dialog to not take input focus with FLAG_NOT_FOCUSABLE so the text editing session where the user is performing input is not disrupted.

And honestly the best thing to do is to have your UI incorporated into the IME itself.

Upvotes: 2

Avram Score
Avram Score

Reputation: 4095

What you want is startActivityForResult(yourIntent, SCAN_VALUE). Your IME will launch the activity, which will then return a value to the IME.

So your service will do something like the following:

Intent yourIntent = new Intent(this , YourActivity.class);
this.startActivityForResult(yourIntent, SCAN_VALUE);

Also in your Service, you'll have to call onActivityResult():

protected void onActivityResult(int requestCode, int resultCode, Intent data)
  {
  switch(requestCode) {
  case SCAN_VALUE:
        if (resultCode == RESULT_OK) {

//TODO: Create some functionality here!

            break;
        }

Okay, so now let's tackle your launched activity. You'll have to modify this to fit your purposes, but here's how you would send a value back from, say, a ListAdapter. You didn't post your code, so it's tough to know exactly what you're doing.

  Object o = this.getListAdapter().getItem(position);
  String scanValue = o.toString();
  Intent returnIntent = new Intent();
  returnIntent.putExtra("ScanValue", scanValue);
  setResult(RESULT_OK,returnIntent);       
  finish();

This will return a value to the IME launching the activity as a string. Again, if you post your code, I can give you more exact instructions. I hope this helps you wrap your mind around startActivityForResult() in the meantime, though.

Upvotes: -2

Related Questions