【发布时间】:2013-09-23 18:12:27
【问题描述】:
将字符串变量从一个应用程序传递到另一个应用程序并返回值的最简单方法是什么?我可以访问这两个应用程序的源代码,但它必须是两个不同的应用程序。
我尝试使用 startActivityForResult,但这似乎只适用于同一应用程序的活动之间。从不同的包调用活动时,startActivityForResult 立即返回 RESULT_CANCELED。似乎有可能通过服务来解决这个问题,但是对于一些字符串变量来说是不是有点过大了?
有没有简单干净的方法来做到这一点?
这是我尝试用于 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) } 错误。我做错了什么?
【问题讨论】:
-
可以把这两个活动的代码贴出来吗?
-
startActivityForResult 应该可以工作...问题似乎在您的代码中
-
对我来说这似乎用 startActivityForResult() 是不可能的。它适用于同一应用程序的活动之间,即使使用不同的包,但不适用于两个不同的应用程序。见link
-
从清单中删除
<data android:mimeType="text/plain" />部分
标签: android android-intent android-activity