版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_36570478/article/details/81324698
提示:在启动一个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://...">