Android开发:显式/隐式Intent意图跳转Activity总结
2015-04-16 10:37
501 查看
显式跳转
是在已知包名和类名的情况下常用的跳转方法:Intent mIntent = new Intent(); mIntent.setClassName("com.android.settings","com.android.settings.Settings"); mContext.startActivity(mIntent);
我们也常这么用:
Intent intent = new Intent(mContext, XXActivity.class); startActivity(intent);这是跳转到当前应用的某个Activity,相信大家都十分熟悉,今天主要讲的是如何使用隐式intent意图跳转
隐式跳转
1、隐式跳转之Action跳转
假定有一个Activity在清单中是这样的声明的:<activity android:name=".ActionActivity"; <intent-filter action android:name="customer_action_here" </intent-filter> </activity>
那么我们就可以使用以下代码进行跳转到上面这个Activity中:
//创建一个隐式的 Intent 对象:Action 动作 Intent intent = new Intent(); //设置 Intent 的动作为清单中指定的action intent.setAction("customer_action_here"); startActivity(intent);
2、隐式跳转之Category跳转
假定有一个Activity在清单中是这样声明的:<activity android:name=".CategoryActivity" > <intent-filter> <action android:name="customer_action_here" /> <category android:name="customer_category_here" /> </intent-filter> </activity>
我们可以使用如下代码进行跳转到以上Activity:
//创建一个隐式的 Intent 对象:Category 类别 Intent intent = new Intent(); intent.setAction("customer_action_here"); //添加与清单中相同的自定义category intent.addCategory("customer_category_here"); startActivity(intent);
3、隐式跳转之Data跳转
假定有一个Activity是这样定义的:< activity android:name=".DataActivity"> < intent-filter> < category android:name="android.intent.category.DEFAULT" /> < data android:scheme="content" android:host="com.example.intentdemo" android:port="8080" android:pathPattern=".*pdf" android:mimeType="text/plain"/> < /intent-filter> < /activity>
我们可以使用如下代码进行跳转:
//创建一个隐式的 Intent 对象,方法四:Date 数据 Intent intent = new Intent(); Uri uri = Uri.parse("content://com.example.intentdemo:8080/abc.pdf"); //注:setData、setDataAndType、setType 这三种方法只能单独使用,不可共用 //单独以 setData 方法设置 URI //intent.setData(uri); //单独以 seType 方法设置 Type //intent.setType("text/plain"); //上面分步骤设置是错误的,要么以 setDataAndType 方法设置 URI 及 mime type intent.setDataAndType(uri, "text/plain"); startActivity(intent);
清单中的port及以下属性时可选的,没有必要一定添加,但是添加了port及以下属性的话,java代码中的Uri中要做相应的匹配。
4、隐式跳转之调用系统应用
4.1 使用浏览器浏览网页
//web浏览器 Uri uri= Uri.parse("http://www.baidu.com"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent);
4.2 调用地图
//打开地图查看经纬度 Uri uri = Uri.parse("geo:38.899533,-77.036476"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent);
4.3 调用电话拨号(不需要拨号权限)
Uri uri = Uri.parse("tel:10086"); Intent intent = new Intent(Intent.ACTION_DIAL, uri);//注意区别于下面4.4的action startActivity(intent);
4.4 调用电话直接拨号(需要拨号权限)
Uri uri = Uri.parse("tel:15980665805"); Intent intent = new Intent(Intent.ACTION_CALL, uri);//注意区别于上面4.3的aciton startActivity(intent);
4.5 调用短信程序(无需发送短信权限,接收者自填)
Intent intent = new Intent(Intent.ACTION_VIEW); intent.putExtra("sms_body", "这里写短信内容"); intent.setType("vnd.android-dir/mms-sms"); startActivity(intent);
4.6 调用短信程序(无需发送短信权限)
Uri uri = Uri.parse("smsto:10086");//指定接收者 Intent intent = new Intent(Intent.ACTION_SENDTO, uri); intent.putExtra("sms_body", "你这个黑心运营商"); startActivity(intent);
4.7 调用邮件程序
Intent intent = new Intent(Intent.ACTION_SENDTO); intent.setData(Uri.parse("mailto:xxx@gmail.com")); intent.putExtra(Intent.EXTRA_SUBJECT, "这是标题"); intent.putExtra(Intent.EXTRA_TEXT, "这是内容"); startActivity(intent);
4.8 调用音乐播放器
Intent intent = new Intent(Intent.ACTION_VIEW); //Uri uri = Uri.parse("file:///sdcard/xiong_it.mp4"); Uri uri = Uri.parse("file:///sdcard/xiong_it.mp3"); intent.setDataAndType(uri, "audio/mp3"); startActivity(intent);
4.9 调用视频播放器
Intent intent = new Intent(Intent.ACTION_VIEW); //Uri uri = Uri.parse("file:///sdcard/xiong_it.mp3"); Uri uri = Uri.parse("file:///sdcard/xiong_it.mp4"); intent.setDataAndType(uri, "video/mp4"); startActivity(intent);
调用视频播放器和音乐播放器的区别在setDataAndType()时一个是audio类型,一个是video类型,很容易记住,不允许使用其他意思相近的单词代替,代替无效。
4.10 调用搜索
Intent intent = new Intent(); intent.setAction(Intent.ACTION_WEB_SEARCH); intent.putExtra(SearchManager.QUERY, "android"); startActivity(intent);
总结
相信大家经过上面的介绍,已经对Intent跳转有了一个比较成熟的理解,Intent是组件之间的纽带,使用它可以让系统替我们完成很多工作,不需要我们来指定完成工作的程序。实际上,我们会发现,调用系统程序使用液无非是隐式跳转,只不过使用的是系统内置的一些Action,Uri,Data等信息而已。希望对大家有所帮助。转载请注明出处和链接:/article/2590628.html
这篇文章是的《Android开发:如何隐藏自己的app应用》续篇,感兴趣的朋友可以查看。
相关文章推荐
- (转)Android开发:显式/隐式Intent意图跳转Activity总结
- Android开发:显式/隐式Intent意图跳转Activity总结
- Android - 界面跳转,显式/隐式意图,四大组件之Activity,Activity生命周期,横竖屏,任务栈启动模式
- Activity详解 Intent显式跳转和隐式跳转, 及多个Activity之间传值 总结
- Activity详解 Intent显式跳转和隐式跳转, 及多个Activity之间传值 总结
- Android开发10——Activity的跳转与传值_显示意图(intent)的应用
- Activity详解 Intent显式跳转和隐式跳转, 及多个Activity之间传值 总结
- Activity详解 Intent显式跳转和隐式跳转
- Activity详解 Intent显式跳转和隐式跳转
- intent 显式意图以及隐式意图,在Activity、Service和Broadcastreceiver应用
- Android显式意图、隐式意图、意图过滤器(intent-filter)、意图间传值
- Android Intent学习之 隐式意图& 显示意图& intent 跳转问题
- Android显式意图、隐式意图、意图过滤器(intent-filter)、意图间传值
- Activity详解 Intent显式跳转和隐式跳转, 及多个Activity之间传值 总 结
- Android实例demo19之Intent显式意图隐式意图、隐式意图打开多个网页
- Android Activity的跳转:普通跳转(显式跳转,隐式跳转)和传值跳转
- Android中Intent的详解(显式,隐式,系统,startActivityForResult);
- 无废话Android之smartimageview使用、android多线程下载、显式意图激活另外一个activity,检查网络是否可用定位到网络的位置、隐式意图激活另外一个activity、隐式意图的配置,自定义隐式意图、在不同activity之间数据传递(5)
- Android之项目开发中在app内Intent意图跳转自己指定的Html5页面
- 移动开发 - Android - 实现两个页面(Activity)的简单跳转(Intent)