How to use putExtra to open another app and send some data for it?

不羁岁月 提交于 2020-02-21 13:46:36

问题


I have two apps (A, B) that I want to link them together. When the user is in app A, by clicking on a button I want to open app B and send some data to it.

I used this method in app A to go app B and send some data to app B:

public static boolean openApp(Context mContext, String packageName) {
    PackageManager manager = context.getPackageManager();
    Intent goToEncyclopedia = manager.getLaunchIntentForPackage(packageName);
    if (goToEncyclopedia == null) {
        return false;
    }
    goToEncyclopedia.addCategory(Intent.CATEGORY_LAUNCHER);
    goToEncyclopedia.putExtra("NAME" , "Ehsan");
    context.startActivity(goToEncyclopedia);

    return true;
}

and I Call it like this in app A:

openApp(mContext, "encyclopedia.rasad.app.codenevisha.com.encyclopedia");

When I call this method it will open app B but data that I want to send with putExtra will not send.

And this is my code in App B to receive data from intent:

Bundle bundle = getIntent().getExtras();
if (bundle != null){
    String name = bundle.getString("NAME");
    Log.i("EXTRAS", name);
}

回答1:


Add intent filter in app B:

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

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

Pass data from app A:

        Intent intent = new Intent("com.yourpackage.action");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("data","data string");
        startActivity(intent);

Retrieve data from app B:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getIntent().getStringExtra("data");
    }



回答2:


Answer 1

In App A use Bundle to send data to App B

Bundle bundle = new Bundle();
bundle.putString("NAME" , "Ehsan");
goToEncyclopedia.putExtras(bundle);

Answer 2

Remove below line of code

goToEncyclopedia.addCategory(Intent.CATEGORY_LAUNCHER);

Final code will be App A

Intent sendIntent =   getPackageManager().getLaunchIntentForPackage("com.example.app2");
sendIntent.putExtra("NAME", "Ehsan");
startActivity(sendIntent);

App B

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent = getIntent();

if (intent.hasExtra("NAME")) {
        String name = intent.getStringExtra("NAME");

}


来源:https://stackoverflow.com/questions/45433120/how-to-use-putextra-to-open-another-app-and-send-some-data-for-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!