Android中通过其他APP启动Activity的四种方式

提示:在启动一个Activity前进行必要的存在检测很有必要,以免程序意外崩溃。

第一种:通过applicationId与package+activityPath

applicationId告诉系统活动在那个App内,进入App内就需要类路径找具体的Activity。

//applicationId:com.example.student0.caller CallerActivity.java

private final static int REQ_CODE = 0X2233;
private final static String TARGET_APP_ID = "com.example.student0.recipient";
private final static String TARGET_ACTIVITY_DIR = "com.student0.demo.RecipientActivity";

ComponentName component = new ComponentName(TARGET_APP_ID, TARGET_ACTIVITY_DIR);
Intent intent = new Intent();
intent.setComponent(component);
startActivityForResult(CallerActivity.this, intent);



//applicationId:com.example.student0.recipient Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.student0.demo">
...
<activity android:name=".RecipientActivity.java" android:exported="true"/>
...

第二种:通过自定义Action启动。

//applicationId:com.example.student0.recipient Manifest.xml
<intent-filter>
    <action android:name="com.example.student0.helloWord"/>
    <category android:name="android.intent.category.DEFAULT"/>
</intent-filter>



//applicationId:com.example.student0.caller CallerActivity.java
String targetActivityAction = "com.example.student0.helloWord";//与被调用的Activity的Action一致
Intent intent = new Intent();
intent.setAction(targetActivityAction);
CallerActivity.this.startActivity(intent);

第三种:通过packageManager获取拥有对应软件包名(ApplicationId)的App的Launch活动。

//applicationId:com.example.student0.caller CallerActivity.java
Intent intent = getPackageManager().getLaunchIntentForPackage("com.example.student0.recipient");
this.startActivity(intent);

第四种:通过<data/>设置可以响应的指定数据类型。

//applicationId:com.example.student0.recipient Mainfest.xml
<intent-filter>
    <category android:name="android.intent.category.DEFAULT"/>
    <data android:scheme="gugu"/>
</intent-filter>

//applicationId:com.example.student0.caller CallerActivity.java

Uri uri = Uri.parse("gugu://...");
Intent intent = new Intent();
intent.setData(uri);

android:scheme。指定数据的协议部分。

android:host。指定数据的主机部分。

android:port。指定数据的端口部分。

android:path。指定主机名和端口后的部分,即相对路径。

android:mimeType。指定可以处理的数据类型,允许使用通配符的方式进行指定。

一个activity可以响应多个android:scheme,当一条<data/>中同时存在host、port、path时,数据的格式需要满足该条<data/>中指定的所有协定。<data/>属性指定的响应数据类型,在网页中也能得到响应例如:

<a href="gugu://...">

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值