2020-03-01-唤醒安卓应用并传参

流程:AppA点击按钮,唤醒AppB并传递参数“hello”。AppB收到“hello”后更新界面,点击AppB上按钮后,唤醒AppA并传递参数“world”。AppA收到“world”后更新界面。

  1. AppA点击按钮
public void f2(final String data) {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.setComponent(new ComponentName("com.app.b","org.cocos2dx.cpp.AppActivity"));
    intent.putExtra("a2b",data);
    startActivity(intent);
}
  1. AppB接收参数
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.setEnableVirtualButton(false);
    super.onCreate(savedInstanceState);
    getAppData();//接收参数一
}
private void getAppData() {
    Intent intent = getIntent();
    final String str_a2b = intent.getStringExtra("a2b");
    if (str_a2b != null) {
        showMsg(str_a2b);
    }
}
public void showMsg(final String msg) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            android.widget.Toast.makeText(AppActivity.this, msg, Toast.LENGTH_LONG).show();
        }
    });
}
@Override
protected void onNewIntent(Intent intent) {//接收参数二
    super.onNewIntent(intent);
    final String str_a2b = intent.getStringExtra("a2b");
    if (str_a2b != null) {
        showMsg(str_a2b);
    }
}
  1. AppB点击按钮和AppA接收参数跟上面写法类似
  2. 关于c++和java如何相互调用,参考https://www.cnblogs.com/liugangBlog/p/6434542.html

你可能感兴趣的:(2020-03-01-唤醒安卓应用并传参)