android开发(26) 和其他应用交换数据方式一,使用intent指定自定义action调用其他程序里的activity,并获得其返回的结果
来源:互联网 发布:淘宝如何搜索苍蝇水 编辑:程序博客网 时间:2024/05/16 07:52
我们在开发中会遇到和其他应用的交互情形,下面是一个简单的方式。整个的使用类似“使用intent调用系统自带的拍照应用并获得结果”。
先看页面:
我们看看实现步骤。
第一个应用 DEMO1:
1.创建一个自定义的action, 使用intent发出
String action = "zyf.demo.customAction"; Intent n = new Intent(action);
2.附件一些自定义的内容,使用putExtra
n.putExtra("key1", "hello,这是来自demo1的消息。");
3.可以先检查是否有匹配这个action的内容。
String action = "zyf.demo.customAction"; Intent n = new Intent(action); PackageManager packageManager = getBaseContext().getPackageManager(); final Intent intent = new Intent(action); List<ResolveInfo> resolveInfo = packageManager .queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); if (resolveInfo.size() > 0) { Toast.makeText(getBaseContext(), "找到了匹配的activity", 0).show(); } else{ Toast.makeText(getBaseContext(), "未找到匹配的activity", 0).show(); }
4.使用 startActivityForResult 方法 启动Intent。
startActivityForResult(n, REQUEST_CODE_1);
5.获得结果(来自DEMO2返回的)
final int REQUEST_CODE_1 = 1;@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if (requestCode == REQUEST_CODE_1) {String res = data.getStringExtra("result");Toast.makeText(getBaseContext(), "收到:" + res, 0).show();} elsesuper.onActivityResult(requestCode, resultCode, data);}
第二个应用,DEMO2:
1.新建一个activity用于接收 “上面指定的action”,并在AndroidManifest.xml 文件中注册intent-filter。
<activity android:name="zyf.demo.demo2.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="zyf.demo.customAction"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity>
2.在activity中可以显示来自DEMO1的数据。
private void tryShowExtra() { try { Intent intent = getIntent(); String extra1 = intent.getStringExtra("key1"); txt1.setText(extra1); } catch (Exception e) { txt1.setText(e.getMessage()); } }
3.在activity中处理关闭当前应用时的返回内容
public void onClick(View v){ Intent resultIntent = new Intent(); resultIntent.putExtra("result", "这是来自demo2的结果"); setResult(Activity.RESULT_OK, resultIntent); this.finish(); }
至此,完成。演示代码下载
0 0
- android开发(26) 和其他应用交换数据方式一,使用intent指定自定义action调用其他程序里的activity,并获得其返回的结果
- android开发(26) 和其他应用交换数据方式一,使用intent指定自定义action调用其他程序里的activity,并获得其返回的结果
- android开发(26) 和其他应用交换数据方式一,使用intent指定自定义action调用其他程序里的activity,并获得其返回的结果
- android打开其他应用的activity并返回数据
- Intent和bundle 启动其他Activity并返回结果
- 启动其他的Activity并返回结果
- Android 启动其他Activity并返回结果
- 【Android】启动其他Activity并返回结果
- 启动其他Activity的Fragment并返回结果
- Android程序里调用其他的APK
- Android中启动其他Activity并返回结果
- Android中启动其他Activity并返回结果
- android: startActivityForResult用法(启动其他Activity并返回结果)
- 使用Bundle在Activity之间交换数据、调用另一个Activity并返回结果
- 启动其他Activity并返回结果
- 启动其他Activity并返回结果
- 启动其他Activity并返回结果
- Android中通过Intent调用其他应用的方法
- 放弃winform的窗体吧,改用html作界面,桌面应用程序UI的新的开发方式。
- 【JS】垃圾回收和块级作用域
- mongodb怎样导出数据为csv或者txt格式
- android开发(25) - 推送的实现,使用百度云推送
- 解决spring整合quartz,tomcat关闭后,quartz还在运行
- android开发(26) 和其他应用交换数据方式一,使用intent指定自定义action调用其他程序里的activity,并获得其返回的结果
- iOS 大头针怎么固定在中间,且移动后及时更新位置信息
- mac系统修改终端前缀
- 列表框在页面交互中的应用
- 【Android】四大组件(1)Activity详解
- poj2528线段树+离散化+二分
- android开发(26) 和其他应用交换数据方式一,使用intent指定自定义action调用其他程序里的activity,并获得其返回的结果
- 冒泡排序
- android开发(27) 看看我的手机里都有什么传感器