8

将字符串变量从一个应用程序传递到另一个应用程序并返回值的最简单方法是什么?我可以访问这两个应用程序的源代码,但它必须是两个不同的应用程序。

我尝试使用 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) }错误。我究竟做错了什么?

4

6 回答 6

2

AIDL 是使用接口在两个不同应用程序之间进行通信的一种方式

http://developer.android.com/guide/components/aidl.html

您可以在以下教程 http://manishkpr.webheavens.com/android-aidl-example/中找到一个工作示例

于 2013-09-24T17:56:36.477 回答
1

您可以使用ContentProvider。这是比其他方法更好的方法。

于 2013-09-23T18:32:48.863 回答
0

您可以在属于这些应用程序的两个服务之间交换信使(即使应用程序来自两个不同的包)并使用这些信使进行通信。

于 2014-11-13T13:32:28.707 回答
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");

那里没什么好看的...

应用程序2...在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>
于 2013-09-23T18:35:21.893 回答
0

SharedPreferences在这方面可能会对您有所帮助。

于 2013-09-23T18:16:45.680 回答
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];
        }
    };
}

在这里阅读

于 2015-07-21T11:19:30.327 回答