cómo llamar a la actividad desde inputmethodservice y obtener respuesta

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?

Gracias por cualquier ayuda.

preguntado el 28 de agosto de 11 a las 00:08

3 Respuestas

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.

Respondido 28 ago 11, 06:08

I don't like the "you can't do that because it's bad" answers... What if we need to can startActivityForResult because there's no other way to access any build-in Intent ? - Orabîg

@Orabîg not if an android framework engineer tells you that :) - numan salati

@hackbod would it be possible for you to post a sample on how to use a Dialog with window type set to TYPE_INPUT_METHOD_DIALOG in a service? - EL USO

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()

respondido 17 mar '20, 22:03

Lo que quieres es 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.

Respondido 28 ago 11, 05:08

I think that was not the point. The problem is that startActivityForResult is a method of the class Activity. Thus, it's not available from InputMethodService... :/ - Orabîg

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.