如何通过数据不同的Android应用程序之间左右逢源? [英] How to pass data both ways between different android applications?
问题描述
什么是最简单的方式为通字符串从一个应用程序变量相互影响,并返回值回?余可使用这两个应用程序的源$ C $ C,但它必须是两个不同的应用程序。
What's the easiest way to pass string variables from one application to another and also return values back? I have access to the source code of both apps, but it has to be two different applications.
我试图与startActivityForResult,但这似乎只在同一应用程序的activies之间工作。当从不同的包调用一个活动,startActivityForResult立即用RESULT_CANCELED返回。似乎是解决这个与服务的可能性,但是,这不是有点过大只是一些字符串瓦尔?
I tried with startActivityForResult, but this only seems to work between activies of the same application. When calling an activity from a different package, startActivityForResult returns immediately with RESULT_CANCELED. There seems to be the possibility to solve this with a Service, but isn't that a bit oversized for just some string vars?
有一个简单而干净的方式做到这一点?
Is there an easy and clean way to do this?
下面的codeI试图使用startActivityForResult:
Here the code i tried to use for 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>
任何人看到错误?附录二总是RESULT_CANCELED立即返回
Anybody seeing the mistake? App B always returns immediately with RESULT_CANCELED
编辑: 现在,我得到一个的 android.content.activitynotfoundexception无活动来处理意图{行为= com.example.testapp.MESSAGE(有群众演员)} 的错误。我究竟做错了什么?
Right now I'm getting a android.content.activitynotfoundexception no activity found to handle intent { act=com.example.testapp.MESSAGE (has extras) } error. What am I doing wrong?
推荐答案
您可以使用的 ContentProvider的。这是一个比别人更好的办法。
you can use ContentProvider.This is a better way than others.
这篇关于如何通过数据不同的Android应用程序之间左右逢源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!