前言
今天想跟大家探究一下关于Intent跳转到其他应用的内容,比如用intent打开网页,拨打电话,发送短信,查看网页。比较适合初学者。笔者也是刚接触安卓学不久,希望写的尽量能够通俗易懂,一些内容选材于官方文档,有兴趣可以去学习。
一、用Intent打电话
核心代码如下,intent里面第一个传入的参数是action,第二个是一个uri对象。
Uri number = Uri.parse("tel:12306");
Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
try {
startActivity(callIntent);
} catch (ActivityNotFoundException e) {
// Define what your app should do if no activity can handle the intent.
}
二、使用Intent打开地图
这里的前提是您安装了百度地图(这里就凸显了调用第三方apk带来的方便,因为它们功能完备,稳定性高)
如果你想了解更多关于跳转地图的知识,我觉得这里有一篇博客很好,建议学习
https://blog.csdn.net/u010007428/article/details/80253145
try {
intent = Intent.getIntent("intent://map/direction?" +
"destination=latlng:" + "34.264642646862" + "," + "108.95108518068" + "|name:我的目的地" + //终点
"&mode=driving&" +
"&src=appname#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (ActivityNotFoundException e) {
//Define what your app should do if no activity can handle the intent.
}
startActivity(intent); //启动调用
之后呢,我们使用adb 命令 在手机上截下图片保存到电脑上,再把图片导入到我的博客上
adb shell screencap -p /sdcard/test.png
adb pull /sdcard/test.png D:
三、使用Intent创建日程,打开日历
Intent calendarIntent = new Intent(Intent.ACTION_INSERT, CalendarContract.Events.CONTENT_URI);
Calendar beginTime = Calendar.getInstance();
beginTime.set(2022, 0, 23, 7, 30);
Calendar endTime = Calendar.getInstance();
endTime.set(2022, 0, 23, 10, 30);
//把开始时间和结束时间的数据放在intent里面
calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis());
calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis());
calendarIntent.putExtra(CalendarContract.Events.TITLE, "这是标题");
calendarIntent.putExtra(CalendarContract.Events.EVENT_LOCATION, "输入的地点");
try {
startActivity(calendarIntent);
}catch (ActivityNotFoundException e) {
// 没有该应用之后怎么做
}
运行之后结果如图
总结
这里你只需了解intent可以打开其他应用,还有更多,比如打开相机,发送短信等等,这里只举几个例子。