Android中隐式Intent及支持库中的IntentBuilder使用示例
来源:互联网 发布:discuz省市区数据库 编辑:程序博客网 时间:2024/06/07 17:07
Android 5.0以后,不能以隐式Intent的方式启动Service,
但仍然可以用隐式Intent来启动Activity。
对应的代码类似于:
..........//指定动作Intent i = new Intent(Intent.ACTION_SEND);//指定数据类型i.setType("text/plain");//放入数据i.putExtra(Intent.EXTRA_TEXT, getCrimeReport());i.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.crime_report_subject));//隐式Intent可能被多个Activity响应,因此可以显示创建一个选择器i = Intent.createChooser(i, getString(R.string.send_report));//拉起ActivitystartActivity(i);..........
上面的内容比较容易,我们主要看看Intent的createChooser方法:
public static Intent createChooser(Intent target, CharSequence title) { return createChooser(target, title, null);}public static Intent createChooser(Intent target, CharSequence title, IntentSender sender) { //实际上就是显示的拉起Chooser Intent intent = new Intent(ACTION_CHOOSER); //将目的Intent当作数据放入chooser intent.putExtra(EXTRA_INTENT, target); //chooser将处理title信息 if (title != null) { intent.putExtra(EXTRA_TITLE, title); } //根据target中的信息,进一步调整intent ............. return intent;}
从这段代码可以看出,显示创建选择器,其实就是显示的拉起系统的Chooser Activity,
不过可以自己定制Chooser Activity的title(个人测试,并不是每个厂商的机器都可以完成定制功能)。
P.S. :
对于构建发送信息的Intent而言,Android的兼容库中定义了ShareCompat.IntentBuilder类。
Android的支持文档中,对应的描述如下:
IntentBuilder is a helper for constructing ACTION_SEND and ACTION_SEND_MULTIPLE sharing intents and starting activities to share content.
The ComponentName and package name of the calling activity will be included.
对于上述的代码,使用IntentBuilder的示例代码如下:
.............mReportButton = (Button) v.findViewById(R.id.crime_report);mReportButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ShareCompat.IntentBuilder intentBuilder = ShareCompat.IntentBuilder.from(getActivity()); intentBuilder.setType("text/plain"); intentBuilder.setSubject(getString(R.string.crime_report_subject)); intentBuilder.setText(getCrimeReport()); intentBuilder.setChooserTitle(R.string.send_report); //通过chooser来拉起真正的目的Activity intentBuilder.startChooser(); }});..............
0 0
- Android中隐式Intent及支持库中的IntentBuilder使用示例
- android intent使用示例
- Android 给Fragment中的控件添加监听及使用intent
- Android中的Intent filter使用
- Android中的Intent简单使用
- android文件打开及Intent及使用
- 【Android】Intent介绍及Intent在Activity中的使用方法
- android ---Intent示例大全
- Android开发常用的Intent的URI及示例
- Android开发之 常用的Intent的URI及示例
- Android开发常用的Intent的URI及示例
- Android 常用的Intent的URI及示例
- Android 常用的Intent的URI及示例
- Android 中的Intent在两个Acitvity传递数据示例
- Android 中的Intent在两个Acitvity传递数据示例
- Android---8---Intent及使用Intent传递数据
- Android打开文件的Intent及使用
- Android Intent 基本使用及对象构成
- ceph J版手动安装
- EventBus使用详解(二)——EventBus使用进阶
- MAC OS下错误提示xcrun: error: invalid active developer path
- Android APP VersionCode和VersionName命名规则VersionName的命名规则
- http://www.cnblogs.com/zyw-205520/p/4771253.html
- Android中隐式Intent及支持库中的IntentBuilder使用示例
- 专业工具软件课程说明与资料下载
- 记一次苦逼的服务器被挖矿的清除过程
- Android Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread
- sun.net.ftp.FtpClient介绍
- weka中包管理器打不开问题
- linux进程管理
- Java---Dictionary 字典
- 3D打印机启动加热过慢的一个小技巧.