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);

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值