2012-11-29 14 views
5

Sto sviluppando un'applicazione che dovrebbe restituire del testo all'app che ha avviato l'intento.Avvia attività per risultato da IME

Ma l'app che avvia l'intento è una tastiera IME/soft. Quindi StartActivityForResult non è disponibile perché un IME è un servizio.

Come posso ottenere questo risultato?

Quello che ho ottenuto finora:

tastiera:

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

Altro 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(); 
} 
+0

non è possibile utilizzare un ricevitore di trasmissione per questo? –

risposta

0

si potrebbe usare una ResultReceiver per questo è pensare.

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 
    } 
} 
1

È possibile utilizzare ResultReceiver. Guarda this example, è abbastanza chiaro spiega come funziona.

Problemi correlati