Android中通过其他APP启动Activity的四种方式
16lz
2021-01-26
提示:在启动一个Activity前进行必要的存在检测很有必要,以免程序意外崩溃。
第一种:通过applicationId与package+activityPath
applicationId告诉系统活动在那个App内,进入App内就需要类路径找具体的Activity。
//applicationId:com.example.student0.caller CallerActivity.javaprivate 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......
第二种:通过自定义Action启动。
//applicationId:com.example.student0.recipient Manifest.xml //applicationId:com.example.student0.caller CallerActivity.javaString 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.javaIntent intent = getPackageManager().getLaunchIntentForPackage("com.example.student0.recipient");this.startActivity(intent);
第四种:通过设置可以响应的指定数据类型。
//applicationId:com.example.student0.recipient Mainfest.xml //applicationId:com.example.student0.caller CallerActivity.javaUri uri = Uri.parse("gugu://...");Intent intent = new Intent();intent.setData(uri);
android:scheme。指定数据的协议部分。
android:host。指定数据的主机部分。
android:port。指定数据的端口部分。
android:path。指定主机名和端口后的部分,即相对路径。
android:mimeType。指定可以处理的数据类型,允许使用通配符的方式进行指定。
一个activity可以响应多个android:scheme,当一条中同时存在host、port、path时,数据的格式需要满足该条中指定的所有协定。属性指定的响应数据类型,在网页中也能得到响应例如:
更多相关文章
- Android在ExpandableListView控制的基本使用
- Android(安卓)4.0 新增的显示数据集的桌面控件
- android之数据库和Content Provider(二)
- Android音频数据传输
- android在Fragment中使用ListView显示数据总结
- AsyncTask进度条加载网站数据到ListView
- Android应用程序访问远程数据库(mysql) is not allowed to conne
- Android(安卓)BLE蓝牙开发中读取数据时设置Notify的方法
- Android网络数据xml解析