什么是将字符串变量从一个应用程序传递到另一个应用程序并返回值的最简单方法?我可以访问两个应用程序的源代码,但它必须是两个不同的应用程序。
我尝试使用startActivityForResult,但这似乎仅在同一应用程序的活动之间起作用。当从不同包中调用活动时,startActivityForResult立即返回RESULT_CANCELED。似乎有可能通过服务解决这个问题,但对于只有一些字符串变量来说,这是否有点过度了呢?
有没有一种简单而干净的方法来做到这一点?
以下是我尝试使用startActivityForResult的代码:
我尝试使用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错误,没有找到处理意图{act=com.example.testapp.MESSAGE(有额外信息)}的活动。我做错了什么?
<data android:mimeType="text/plain" />
部分。 - Selvin