与其他应用交互 Intent
来源:互联网 发布:工信部域名备案系统 编辑:程序博客网 时间:2024/05/20 05:56
1 验证是否存在接收 Intent 的应用
PackageManager packageManager = getPackageManager();List activities = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);boolean isIntentSafe = activities.size() > 0;
如果 isIntentSafe 是 true,则至少有一个应用将响应该 Intent。 如果它是 false,则没有任何应用处理该 Intent。
2 如果要执行的操作可由多个应用处理并且用户可能 习惯于每次选择不同的应用 — 比如“共享”操作时,需要显示选择器,请使用 createChooser() 创建Intent 并将其传递给 startActivity()。
Intent intent = new Intent(Intent.ACTION_SEND); ...// Always use string resources for UI text.// This says something like "Share this photo with"String title = getResources().getString(R.string.chooser_title);// Create intent to show chooserIntent chooser = Intent.createChooser(intent, title);// Verify the intent will resolve to at least one activityif (intent.resolveActivity(getPackageManager()) != null) { startActivity(chooser);}
这将显示一个对话框,其中包含响应传递给 createChooser() 方法的 Intent 的应用列表,并且将提供的文本用作对话框标题。
3 如何开始允许用户选择联系人的 Activity:
static final int PICK_CONTACT_REQUEST = 1; // The request code...private void pickContact() { Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts")); pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);}
接收
奖励:读取联系人数据
显示如何从“联系人”应用获取结果的代码不会详细说明如何实际从结果读取数据,但它需要对内容提供程序进行更深入的探讨。 但是,如果您很好奇,此处提供了更多的代码向您展示如何查询结果数据,从所选联系人获取电话号码:
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) { // Check which request it is that we're responding to if (requestCode == PICK_CONTACT_REQUEST) { // Make sure the request was successful if (resultCode == RESULT_OK) { // Get the URI that points to the selected contact Uri contactUri = data.getData(); // We only need the NUMBER column, because there will be only one row in the result String[] projection = {Phone.NUMBER}; // Perform the query on the contact to get the NUMBER column // We don't need a selection or sort order (there's only one result for the given URI) // CAUTION: The query() method should be called from a separate thread to avoid blocking // your app's UI thread. (For simplicity of the sample, this code doesn't do that.) // Consider using CursorLoader to perform the query. Cursor cursor = getContentResolver() .query(contactUri, projection, null, null, null); cursor.moveToFirst(); // Retrieve the phone number from the NUMBER column int column = cursor.getColumnIndex(Phone.NUMBER); String number = cursor.getString(column); // Do something with the phone number... } }}
4支持其他应用Intent请求
<activity android:name="ShareActivity"> <!-- filter for sending text; accepts SENDTO action with sms URI schemes --> <intent-filter> <action android:name="android.intent.action.SENDTO"/> <category android:name="android.intent.category.DEFAULT"/> <data android:scheme="sms" /> <data android:scheme="smsto" /> </intent-filter> <!-- filter for sending text or images; accepts SEND action and text or image data --> <intent-filter> <action android:name="android.intent.action.SEND"/> <category android:name="android.intent.category.DEFAULT"/> <data android:mimeType="image/*"/> <data android:mimeType="text/plain"/> </intent-filter></activity>
阅读全文
0 0
- 与其他应用交互 Intent
- Android利用Intent与其他应用交互
- 【Android training】与其他应用交互 Intent
- android与其他应用的交互
- 5.3 与其他Fragment交互
- 与其他APP进行交互
- 为 Intent extra 定义键时最好使用应用的软件包名称作为前缀。这可以确保在您的应用与其他应用交互过程中这些键始终保持唯一。
- 1.7_Android Training 学习笔记_与其他应用的交互
- Android官方培训课程学习(五): 与其他应用交互
- App与其他应用交互之隐式意图的使用
- (六)Interacting with Other Apps与其他应用进行交互
- android 与其他程序交互(一)
- PHP与其他语言交互-总结
- IOS与其他客户端音频交互
- 实现自己与其他app交互
- H5与其他平台交互框架
- SAP与其他系统交互的方式
- Google Android开发者文档系列-与其他应用程序交互之允许其他应用启动你的Activity
- cordova项目创建过程
- (记录)面试遇到的中间自适应,两端固定的三列布局
- iis7.5 php进程意外退出,错误代码 0xfffffffe
- 使用Eclipse中提供的Refactor(重构)工具提取接口
- CSS3 transform
- 与其他应用交互 Intent
- 点击按钮进行QQ交谈
- 通过intent跳转到系统拨号页面
- jxl excel导出
- Android样式的开发:shape篇
- 【LeetCode算法练习(C++)】3Sum Closest
- 【OpenGL】用Bresenham算法画线
- Android 静默安装 静默卸载 探索之路
- JAVA运算符