Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send data between one application to other application in android?

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>
like image 983
Farzad Avatar asked Jul 27 '16 09:07

Farzad


People also ask

What component is used to share data between two applications?

Content provider is the android component, which has to be used if one application wants to share its data with other application.


1 Answers

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"));
like image 166
Farzad Avatar answered Nov 07 '22 13:11

Farzad