Android开发官方文档---Interacting with Other Apps
来源:互联网 发布:暖气管道改造 知乎 编辑:程序博客网 时间:2024/06/05 02:55
例如:
调用自带浏览器打开指定网页
Uri webpage =Uri.parse("http://www.android.com");
Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
打电话
Uri number =Uri.parse("tel:5551234");
Intent callIntent = newIntent(Intent.ACTION_DIAL, number);
打开地图
// Map point based onaddress
Uri location =Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California");
// Or map point based on latitude/longitude
// Uri location = Uri.parse("geo:37.422219,-122.08364?z=14"); // zparam is zoom level
Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);
发送邮件
Intent emailIntent =new Intent(Intent.ACTION_SEND);
// The intent does not have a URI, so declare the "text/plain" MIMEtype
emailIntent.setType(HTTP.PLAIN_TEXT_TYPE);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"jon@example.com"}); //recipients
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Emailsubject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Email messagetext");
emailIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("content://path/to/email/attachment"));
// You can also attach multiple items by passing an ArrayList of Uris
设置日历
Intent calendarIntent =new Intent(Intent.ACTION_INSERT, Events.CONTENT_URI);
Calendar beginTime = Calendar.getInstance().set(2012, 0, 19, 7,30);
Calendar endTime = Calendar.getInstance().set(2012, 0, 19, 10,30);
calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,beginTime.getTimeInMillis());
calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,endTime.getTimeInMillis());
calendarIntent.putExtra(Events.TITLE, "Ninja class");
calendarIntent.putExtra(Events.EVENT_LOCATION, "Secret dojo");
使用下面代码获取可以使用的应用
PackageManagerpackageManager = getPackageManager();
List<ResolveInfo>activities = packageManager.queryIntentActivities(intent, 0);
boolean isIntentSafe =activities.size() > 0;
返回的 isIntentSafe 就为true,如果找不到应用打开就为false
创建选择器
Intent intent = newIntent(Intent.ACTION_SEND);
...
// Always use stringresources for UI text.
// This says somethinglike "Share this photo with"
String title =getResources().getString(R.string.chooser_title);
// Create intent toshow chooser
Intent chooser =Intent.createChooser(intent, title);
// Verify the intentwill resolve to at least one activity
if(intent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
获取Activity返回数据
static final intPICK_CONTACT_REQUEST = 1; // The request code
...
private voidpickContact() {
Intent pickContactIntent = newIntent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/phone numbers
startActivityForResult(pickContactIntent,PICK_CONTACT_REQUEST);
}
/**
requestCode 请求码
resultCode 结果码
data 返回的带数据的Intent
*/
@Override
protected void onActivityResult(int requestCode, int resultCode,Intent data) {
if (requestCode ==PICK_CONTACT_REQUEST) { //判断请求码
if (resultCode == RESULT_OK) { //判断结果是否成功
Uri contactUri =data.getData();
String[] projection = {Phone.NUMBER};
//query() 操作不要在主线程中使用,避免操作超时停止响应,这里是为了简化才放在主线程
Cursor cursor = getContentResolver() .query(contactUri,projection, null, null, null);
cursor.moveToFirst();
int column = cursor.getColumnIndex(Phone.NUMBER);
String number = cursor.getString(column);
}
}
}
让其它应用可以调用你的Activity
添加Intent Filter
<action> 接受的动作
<category> 类型
<data> 数据类型
<activityandroid:name="ShareActivity">
<!-- filter for sending text;accepts SENDTO action with sms URI schemes -->
<intent-filter>
<actionandroid:name="android.intent.action.SENDTO"/>
<categoryandroid:name="android.intent.category.DEFAULT"/>
<dataandroid:scheme="sms" />
<dataandroid:scheme="smsto" />
</intent-filter>
<!-- filter for sending text orimages; accepts SEND action and text or image data -->
<intent-filter>
<actionandroid:name="android.intent.action.SEND"/>
<categoryandroid:name="android.intent.category.DEFAULT"/>
<dataandroid:mimeType="image/*"/>
<dataandroid:mimeType="text/plain"/>
</intent-filter>
</activity>
注意:
category 默认可以设置成 DEFAULT
在Activity中处理Intent
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get the intent that started thisactivity
Intent intent = getIntent(); //获取请求的Intent对象
Uri data = intent.getData(); //获取Intent携带的数据
// Figure out what to do based onthe intent type
if (intent.getType().indexOf("image/") != -1){ //根据不同的类型执行不同操作
//Handle intents with image data ...
} else if (intent.getType().equals("text/plain")){
//Handle intents with text ...
}
}
返回处理结果
RESULT_OK 或者 RESULT_CANCELED
注意:默认返回的是 RESULT_CANCELED,如果在设置处理结果之前点击返回按钮取消当前的Activity,返回的就是默认值。
Intent result = newIntent("com.example.RESULT_ACTION",Uri.parse("content://result_uri");
setResult(Activity.RESULT_OK,result);
finish(); //如果请求的源Activity调用的是 startActivityForResult,设置完结果之后应该调用finish结束当前的Activity,返回到源Activity.
也可以使用setResult
setResult(RESULT_COLOR_RED);
finish();
返回简单的整数值(大于0)
- Android开发官方文档---Interacting with Other Apps
- android-Interacting with Other Apps
- Interacting with Other Apps
- Interacting with Other Apps笔记
- Android Develop Training——和其它Android应用交互(Interacting with Other Apps)
- Develop--Training(三)Getting Started--Interacting with Other Apps
- 跟Google学写代码:Interacting with Other Apps【Capture Photo from phone】
- (六)Interacting with Other Apps与其他应用进行交互
- 一周文章精选:Android官方文档之Web Apps开发
- 官方文档 恢复备份指南四 Starting and Interacting with the RMAN Client
- Android官方文档之Web Apps调试
- Android官方文档在WebView中构建Web Apps
- Android官方文档之Web Apps最佳实践
- Android官方文档在WebView中构建Web Apps
- Android官方开发中心之Web Apps屏幕适配
- android官方开发文档地址
- Android官方开发文档地址
- android ndk开发官方文档
- Mac上eclipse安装SVN+JavaHL
- Android之自定义属性,format详解
- PowerDesigner(四)-业务处理模型
- Java垃圾收集器与内存分配策略
- (二)hue与HDFS、YARN集成配置与测试
- Android开发官方文档---Interacting with Other Apps
- 扩大数据库表空间
- OC-UI-002.按钮的属性frame、bounds、center
- MYSQL-TIMESTAMP数据类型的默认值与自动更新问题
- 【Android】解析Json对象,用自带json解析器
- IDFA---广告唯一标示
- MAVEN+ANT打包
- Python 基础语法
- iOS 数据缓存