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

前端 未结 6 1083
再見小時候
再見小時候 2020-12-11 18:00

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.

<
相关标签:
6条回答
  • 2020-12-11 18:18

    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"));
    
    0 讨论(0)
  • 2020-12-11 18:18

    My requirement was to send the "user id" from App1 to App2 and get "username" back to App1.

    I needed to launch my app directly without any chooser. I was able to achieve this using implicit intent and startActivityForResult.

    App1 > MainActivity.java

    private void launchOtherApp() {
        Intent sendIntent = new Intent();
        //Need to register your intent filter in App2 in manifest file with same action.
        sendIntent.setAction("com.example.sender.login"); // <packagename.login>
        Bundle bundle = new Bundle();
        bundle.putString("user_id", "1111");
        sendIntent.putExtra("data", bundle);
        sendIntent.setType("text/plain");
        if (sendIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(sendIntent, REQUEST_CODE);
        }
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE) {
            if (resultCode == Activity.RESULT_OK) {
                Bundle bundle = data.getBundleExtra("data");
                String username = bundle.getString("user_name");
                result.success(username);
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
    

    I had two activity in App2 ie. MainActivity and LoginActivity.

    App2 > AndroidManifest.xml

    <activity android:name=".LoginActivity">
        <intent-filter>
            <!--The action has to be same as App1-->
            <action android:name="com.example.sender.login" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/plain" />
        </intent-filter>
    </activity>
    

    Sorry for this I had a little mix up with Java and Kotlin. My second app was in Kotlin, not that it will effect in any way.

    App2 > LoginActivity.java

    override fun onResume() {
        super.onResume()
        var userId = "No data received"
        val intent = intent
        if (intent != null
            && intent.action != null
            && intent.action.equals("com.example.sender.login")
        ) {
            val bundle = intent.getBundleExtra("data")
            if (bundle != null) {
                userId = bundle.getString("user_id")
                userId = " User id is $userId"
            }
        }
        tvMessage.text = "Data Received: $userId"
    }
    
    fun onClickBack(view: View) {
        val intent = intent
        val bundle = Bundle()
        bundle.putString("sesion_id", "2222")
        intent.putExtra("data", bundle)
        setResult(Activity.RESULT_OK, intent)
        finish()
    }
    
    0 讨论(0)
  • 2020-12-11 18:20

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

    0 讨论(0)
  • 2020-12-11 18:26

    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.

    0 讨论(0)
  • 2020-12-11 18:28

    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.

    0 讨论(0)
  • 2020-12-11 18:41

    This should work:

    APP1

        Intent i2 = new Intent();
        i2.setComponent(new ComponentName(PACKAGE,ACTIVITY));//the destination packageName
        i2.putExtra("Id", "100");
        startActivity(i2);
        
    

    APP2

        String myString = getIntent().getStringExtra("Id");
    
    0 讨论(0)
提交回复
热议问题