android点击另一个app,Android 怎么从一个APP中打开另外一个APP

Android 如何从一个APP中打开另外一个APP

众所周知,在一个APP内部,从一个页面跳转到另外一个页面是使用startactivity函数来实现的。

同样的,对于应用之间的跳转也是如此的。应用直接的跳转分为三种情况,为了方便描述,我加上有两个应用A和B,需求是要从A点击一个按钮,可以跳转到B,同时A要传数据给B,B能够接收并且处理:

1.从A跳转到B的主Activity,代码如下:

Intent intent = new Intent(Intent.ACTION_MAIN);

intent.addCategory(Intent.CATEGORY_LAUNCHER);

ComponentName comp = new ComponentName("com.nbg.baby",

"com.nbg.baby.MainActivity");

intent.setComponent(comp);

int launchFlags = Intent.FLAG_ACTIVITY_NEW_TASK

| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED;

intent.setFlags(launchFlags);

intent.setAction("android.intent.action.VIEW");

Bundle bundle = new Bundle();

bundle.putString("from", "来自测试应用");

intent.putExtras(bundle);

startActivity(intent);

1.1如果B是应用是关闭的,那么在onCreate()函数中增加如下语句:

Bundle bundle = this.getIntent().getExtras();

if(bundle!=null && bundle.getString("from")!=null){

Util.showToast(s_instance, bundle.getString("from"), Toast.LENGTH_LONG);

}

1.2 如何B应用是开启状态,那么在onRestart()函数中增加如下语句:

Bundle bundle = this.getIntent().getExtras();

if(bundle!=null && bundle.getString("from")!=null){

Util.showToast(s_instance, bundle.getString("from"), Toast.LENGTH_LONG);

}

2.从A跳转到B的某个被设置为exported的activity,B应用的状态无关紧要,代码如下:

ComponentName comp = new ComponentName("com.nbg.baby",

"com.nbg.baby.ApiLoginActivity");

Intent intent = new Intent();

intent.setComponent(comp);

intent.setAction(Intent.ACTION_VIEW);

Bundle bundle = new Bundle();

bundle.putString("extra", "我来自星星");

intent.putExtras(bundle);

context.startActivityForResult(intent, 1);