Android 开发之:Intent.createChooser() 妙用
大家对该功能第一印象就是ApiDemo 里面的 其只有区区几行代码 提取为:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("audio/*");
startActivity(Intent.createChooser(intent, "Select music"));
执行之 会弹出一个对话框 效果为:

怎么实现这个呢?
1. 定义TestActivity 用于根据传入Uri 播放目标
public class TestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.setTitle("TestActivity");
Intent i = this.getIntent();
Uri u = i.getData();
try {
playMusic(u);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void playMusic(Uri uri) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException{
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(this, uri);
mp.prepare();
mp.start();
}
}
2. 在AndroidManifest 注册TestActivity
<activity android:name=".TestActivity" android:label="TestActivity">
<intent-filter>
<action android:name="android.intent.action.GET_CONTENT" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.OPENABLE" />
<data android:mimeType="audio/music1" />
</intent-filter>
</activity>
3. 使用TestActivity,在外部或者自己程序里调用:
public void sendChooser(){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setDataAndType(Uri.parse("file:///sdcard/DCIM/cc.mp3"), "audio/music1");
startActivity(Intent.createChooser(intent, "Select music1 app"));
}
最后会弹出选择 TestActivity:

又比如,打开地图并直接定为到某个经纬度的地方:
final String uri = String.format("geo:%s,%s?q=%s",
checkIn.getLocation().getLatitude(),
checkIn.getLocation().getLongitude(),
checkIn.getName());
// Show a chooser that allows the user to decide how to display this data, in this case, map data.
startActivity(Intent.createChooser(new Intent(Intent.ACTION_VIEW, Uri.parse(uri)), getString(R.string.choose)));
Android 开发之:Intent.createChooser() 妙用的更多相关文章
- Android开发之Intent跳转到系统应用中的拨号界面、联系人界面、短信界面
现在开发中的功能需要直接跳转到拨号.联系人.短信界面等等,查找了很多资料,自己整理了一下. 1.跳转到拨号界面,代码如下: 1)直接拨打 Intent intentPhone = new Intent ...
- android开发之Intent.setFlags()_让Android点击通知栏信息后返回正在运行的程序
android开发之Intent.setFlags()_让Android点击通知栏信息后返回正在运行的程序 在应用里使用了后台服务,并且在通知栏推送了消息,希望点击这个消息回到activity ...
- Android开发之Intent略解
Intent是一种运行时绑定(run-time binding)机制,它能在程序运行过程中连接两个不同的组件.通过Intent,你的程序可以向Android表达某种请求或者意愿,Android会根据意 ...
- Android开发之Intent.Action
1 Intent.ACTION_MAIN String: android.intent.action.MAIN 标识Activity为一个程序的開始.比較经常使用. Input:nothing Out ...
- Android开发之Intent.Action 各种Action的常见作用
1 Intent.ACTION_MAIN String: android.intent.action.MAIN 标识Activity为一个程序的开始.比较常用. Input:nothing Outpu ...
- Android开发之Intent.Action Android中Intent的各种常见作用
1 Intent.ACTION_MAIN String: android.intent.action.MAIN 标识Activity为一个程序的开始.比较常用. Input:nothing Outpu ...
- Android开发之Intent的传值--Application
每当我们想要将输入的值传递到多个界面时,只是使用Intent传值的话,就会有一些的弊端. 下面我就以三个页面为例,进行简单的说明一下: 思路: 1.第一个页面是客户输入相关的信息. 2.将客户输入的信 ...
- Android开发之旅: Intents和Intent Filters(理论部分)
引言 大部分移动设备平台上的应用程序都运行在他们自己的沙盒中.他们彼此之间互相隔离,并且严格限制应用程序与硬件和原始组件之间的交互. 我们知道交流是多么的重要,作为一个孤岛没有交流的东西,一定毫无意义 ...
- 【Android UI】Android开发之View的几种布局方式及实践
引言 通过前面两篇: Android 开发之旅:又见Hello World! Android 开发之旅:深入分析布局文件&又是“Hello World!” 我们对Android应用程序运行原理 ...
- Android开发之TextView高级应用
Android开发之TextView高级应用 我们平时使用TextView往往让它作为一个显示文字的容器,但TextView的功能并不局限于此.以下就和大家分享一下TextView的一些使用技巧. A ...
随机推荐
- Python标准库:内置函数chr(i)
返回一个參数i表示的字符串. 比方,chr(97)返回字符"a".參数i的有效范围为0到1.114,111(0x10FFFF),其他范围的值会抛出异常ValueError. 与之相 ...
- 在modelsim中加入quartus仿真库
找到modelsim安装目录下的modelsim.ini文件. 将modelsim.ini的只读属性去掉. 打开quartus软件.选择Launch Simulation Library Compil ...
- 1.const
在C++中,const 的含义并没有改变,只是对细节进行了一些调整,以下是最主要的两点. 一.C++中的 const 更像编译阶段的 #define 先来看下面的两条语句: ; int n = m; ...
- eclipse + MinGW调试程序printf输出被buffer的问题
比如说一个 1. int x = 1;2. printf("xxx")3. int y = 2;调试的时候,运行第二行,但是控制台没有输出.必须调试到整个程序都结束的时候才会把所有 ...
- Idea 2017的激活方式
https://blog.csdn.net/wangyuanjun008/article/details/79233491
- Java并发编程(十二)线程安全性的委托
在组合对象中如果每个组件都已经是线程安全的,是否需要再加一个额外的"线程安全层",需要视情况而定. final可以修饰未复制的属性,只要在静态代码块或者构造函数中赋值了即可. 独立 ...
- openwrt安装编译
官网安装编译推荐: https://wiki.openwrt.org/doc/howto/buildroot.exigence https://wiki.openwrt.org/doc/howto/b ...
- JS方法代理
作者:Jiang, Jilin JS作为一门脚本语言.十分easy上手.外加其灵活性,能够轻而易举地扩展功能.今天,我们就聊聊JS的方法代理. 方法代理是脚本语言中常见的方法扩展形式.这样的灵活的形式 ...
- python 使用正则表达式的爬虫
下面我们一起尝试一下爬取内涵段子网站: http://www.neihan8.com/article/list_5_1.html 打开之后,不难看到里面一个一个灰常有内涵的段子,当你进行翻页的时候,注 ...
- 使用nginx cache缓存网站数据实践
Nginx本身就有缓存功能,能够缓存静态对象,比如图片.CSS.JS等内容直接缓存到本地,下次访问相同对象时,直接从缓存即可,无需访问后端静态服务器以及存储存储服务器,可以替代squid功能. 1 ...