Domanda

I'm developing an app that should return some text to the app that started the intent.

But the app that starts the intent is a IME/soft Keyboard. So StartActivityForResult is not available because an IME is a service.

How can I achieve this?

What I got so far:

Keyboard:

final Intent intent = new Intent("com.example.helloworld.GETTEXT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
intent.putExtra("keyboard", true);
startActivity(intent);

Other App:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Bundle extras = getIntent().getExtras();
    if (extras == null){                
        return;
    } else {
        finish();
    }
}

@Override
public void finish() {
    Intent data = new Intent();
    data.putExtra("test", "PASSED");
    setResult(RESULT_OK, data);
    super.finish();
}

Nessuna soluzione corretta

Altri suggerimenti

You can use ResultReceiver. Look at this example, it's pretty clear explains how it works.

You could use a ResultReceiver for this is think.

ResultReceiver lReceiver = new KeyboardResultReceiver(aListener);
final Intent intent = new Intent("com.example.helloworld.GETTEXT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
intent.putExtra(EXTRA_RESULT_RECIEVER, lReceiver);
intent.putExtra("keyboard", true);
startActivity(intent);

private static final class KeyboardResultReceiver extends ResultReceiver {

    public FileUploadResultReceiver() {
    }

    @Override
    protected void onReceiveResult(int aResultCode, Bundle aResultData) {
            //Do your thing here you can also use the bundle for your data transmission
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top