How to send data between one application to other

2019-02-24 00:07发布

i've tried to sending data between App1 to App2 via Intent in Android

i used this code but i couldn't resolve my problem.

App1 MainActivity :

        Intent i2 = new Intent("com.appstore.MainActivity");
        i2.setPackage("com.appstore");//the destination packageName
        i2.putExtra("Id", "100");
        startActivity(i2);

App2 MainActivity :

Bundle data = getIntent().getExtras;
if(data!=null){
    String myString = b.getString("Id");

}

Manfiest App2 MainActivity:

   <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="image/*" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND_MULTIPLE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="image/*" />
            </intent-filter>

        </activity>

4条回答
We Are One
2楼-- · 2019-02-24 00:21

Content providers:

Content providers are the standard interface that connects data in one process with code running in another process.

See Android Docs.

Content provider working demo here.

查看更多
▲ chillily
3楼-- · 2019-02-24 00:33

Final code:

App 1 :

        Intent intent = new Intent();
        intent.setClassName("com.appstore", "com.appstore.MyBroadcastReceiver");
        intent.setAction("com.appstore.MyBroadcastReceiver");
        intent.putExtra("KeyName","code1id");
        sendBroadcast(intent);

App 2:

Reciver:
public class MyBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Data Received from External App", Toast.LENGTH_SHORT).show();

    }
}

Manifest :

        <receiver
            android:name=".MyBroadcastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="first_app_packagename" />
            </intent-filter>
        </receiver>

MainActivity :

  MyBroadcastReceiver mReceiver = new MyBroadcastReceiver();
        registerReceiver(mReceiver,
                new IntentFilter("first_app_packagename"));
查看更多
Evening l夕情丶
4楼-- · 2019-02-24 00:37

When you do this:

    Intent i2 = new Intent("com.appstore.MainActivity");
    i2.setPackage("com.appstore");//the destination packageName
    i2.putExtra("Id", "100");
    startActivity(i2);

you are calling the single-argument constructor of Intent. In this constructor, the argument is interpreted as the Intent ACTION. You then set the package name in the Intent.

When you call startActivity() with this Intent, Android will look for an Activity that contains an <intent-filter> with the specified ACTION. There are no installed applications that have an Activity defined like this in the manifest:

<activity>
    <intent-filter>
        <action android:name="com.appstore.MainActivity"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>

So Android will not be able to find and launch the Activity that you want.

As you want to specify explicitly the component that you want to use, instead of using the 1-argument Intent constructor, you should do this instead:

    Intent i2 = new Intent();
    i2.setClassName("com.appstore", "com.appstore.MainActivity");
    i2.putExtra("Id", "100");
    startActivity(i2);

Using setClassName() you provide the package name and the class name of the component that you want to launch.

查看更多
贼婆χ
5楼-- · 2019-02-24 00:43

Using Bundle.putSerializable(Key,Object); and Bundle.putParcelable(Key, Object); the former object must implement Serializable, and the latter object must implement Parcelable.

查看更多
登录 后发表回答