あるアプリケーションから別のアプリケーションに文字列変数を渡し、値を返す最も簡単な方法は何ですか? 両方のアプリのソース コードにアクセスできますが、2 つの異なるアプリケーションである必要があります。
startActivityForResult を試してみましたが、これは同じアプリケーションのアクティビティ間でしか機能しないようです。別のパッケージからアクティビティを呼び出すと、startActivityForResult はすぐに RESULT_CANCELED を返します。これを Service で解決できる可能性があるようですが、文字列変数だけでは少し大きすぎませんか?
これを行う簡単でクリーンな方法はありますか?
ここで、startActivityForResult に使用しようとしたコード:
//App A:
Intent intent = new Intent();
intent.setAction("com.example.testapp.MESSAGE");
Bundle b = new Bundle();
b.putString("loginToken", "263bhqw3jhf6as4yf8j0agtz8h2hj2z9j3hg3g3ggh34uzh2h2ui78h3i9wdnj89x");
intent.putExtra("MyData", b);
startActivityForResult(intent, TEST_REQUEST);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("pairing", "onActivityResult called");
// Check which request we're responding to
if (requestCode == TEST_REQUEST) {
// Make sure the request was successful
Log.d("pairing", "got result, resultCode: " + resultCode);
if (resultCode == RESULT_OK) {
// The Intent's data Uri identifies which contact was selected.
if (data.hasExtra("returnMessage")) {
Toast.makeText(this, data.getExtras().getString("returnMessage"), Toast.LENGTH_LONG).show();
}
}
}
}
// App B:
Intent result = new Intent();
Bundle b = new Bundle();
b.putString("returnValue", "this is the returned value");
result.putExtra("MyData", b);
setResult(Activity.RESULT_OK, result);
Log.d("pairing", "RESULT_OK set");
finish();
//App B Manifest
<activity
android:name="com.example.testapp"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan" >
<intent-filter>
<action android:name="com.example.testapp.MESSAGE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter></activity>
間違いを見た人はいますか?アプリ B は常に RESULT_CANCELED ですぐに戻ります
編集:現在、android.content.activitynotfoundexception no activity found to handle intent { act=com.example.testapp.MESSAGE (has extras) }エラーが発生しています。私は何を間違っていますか?