将字符串变量从一个应用程序传递到另一个应用程序并返回值的最简单方法是什么?我可以访问这两个应用程序的源代码,但它必须是两个不同的应用程序。
我尝试使用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(has extras)} 错误。我做错了什么?
答案 0 :(得分:2)
AIDL是使用接口
在两个不同应用程序之间进行通信的一种方式http://developer.android.com/guide/components/aidl.html
您可以在下面的教程中找到工作示例 http://manishkpr.webheavens.com/android-aidl-example/
答案 1 :(得分:1)
你可以使用ContentProvider。这是比其他人更好的方法。
答案 2 :(得分:0)
SharedPreferences
可能会在这方面为您提供帮助。
答案 3 :(得分:0)
我有两个将数据传入/传出的应用程序。
App1...
Intent i = new Intent("com.xxx.yyy.MESSAGE");
Bundle b = new Bundle();
b.putString("AAA", getAAA());
i.putExtra("MyData", b);
startActivityForResult(i, "myProcess");
那里没什么好看的......
App {... onResume()
...
Intent i = getIntent();
if (i != null && i.getAction().equals("com.xxx.yyy.MESSAGE") {
...get the data from the bundle
}
请注意,AndroidManifest.xml(适用于App2)具有以下其中一项活动的条目
<intent-filter>
<action android:name="com.xxx.yyy.MESSAGE"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
答案 4 :(得分:0)
您可以在属于这些应用程序的两个服务之间交换信使(即使应用程序来自两个不同的软件包),并使用这些信使进行通信。
答案 5 :(得分:0)
public class MyParcelable implements Parcelable {
// You can include parcel data types
private int mData;
private String mName;
// We can also include child Parcelable objects. Assume MySubParcel is such a Parcelable:
private MySubParcelable mInfo;
// This is where you write the values you want to save to the `Parcel`.
// The `Parcel` class has methods defined to help you save all of your values.
// Note that there are only methods defined for simple values, lists, and other Parcelable objects.
// You may need to make several classes Parcelable to send the data you want.
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeInt(mData);
out.writeString(mName);
out.writeParcelable(mInfo, flags)
}
// Using the `in` variable, we can retrieve the values that
// we originally wrote into the `Parcel`. This constructor is usually
// private so that only the `CREATOR` field can access.
private MyParcelable(Parcel in) {
mData = in.readInt();
mName = in.readString();
mInfo = in.readParcelable(MySubParcelable.class.getClassLoader());
}
public MyParcelable() {
// Normal actions performed by class, since this is still a normal object!
}
// In the vast majority of cases you can simply return 0 for this.
// There are cases where you need to use the constant `CONTENTS_FILE_DESCRIPTOR`
// But this is out of scope of this tutorial
@Override
public int describeContents() {
return 0;
}
// After implementing the `Parcelable` interface, we need to create the
// `Parcelable.Creator<MyParcelable> CREATOR` constant for our class;
// Notice how it has our class specified as its type.
public static final Parcelable.Creator<MyParcelable> CREATOR
= new Parcelable.Creator<MyParcelable>() {
// This simply calls our new constructor (typically private) and
// passes along the unmarshalled `Parcel`, and then returns the new object!
@Override
public MyParcelable createFromParcel(Parcel in) {
return new MyParcelable(in);
}
// We just need to copy this and change the type to match our class.
@Override
public MyParcelable[] newArray(int size) {
return new MyParcelable[size];
}
};
}