大家对该功能第一印象就是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() 妙用的更多相关文章

  1. Android开发之Intent跳转到系统应用中的拨号界面、联系人界面、短信界面

    现在开发中的功能需要直接跳转到拨号.联系人.短信界面等等,查找了很多资料,自己整理了一下. 1.跳转到拨号界面,代码如下: 1)直接拨打 Intent intentPhone = new Intent ...

  2. android开发之Intent.setFlags()_让Android点击通知栏信息后返回正在运行的程序

    android开发之Intent.setFlags()_让Android点击通知栏信息后返回正在运行的程序     在应用里使用了后台服务,并且在通知栏推送了消息,希望点击这个消息回到activity ...

  3. Android开发之Intent略解

    Intent是一种运行时绑定(run-time binding)机制,它能在程序运行过程中连接两个不同的组件.通过Intent,你的程序可以向Android表达某种请求或者意愿,Android会根据意 ...

  4. Android开发之Intent.Action

    1 Intent.ACTION_MAIN String: android.intent.action.MAIN 标识Activity为一个程序的開始.比較经常使用. Input:nothing Out ...

  5. Android开发之Intent.Action 各种Action的常见作用

    1 Intent.ACTION_MAIN String: android.intent.action.MAIN 标识Activity为一个程序的开始.比较常用. Input:nothing Outpu ...

  6. Android开发之Intent.Action Android中Intent的各种常见作用

    1 Intent.ACTION_MAIN String: android.intent.action.MAIN 标识Activity为一个程序的开始.比较常用. Input:nothing Outpu ...

  7. Android开发之Intent的传值--Application

    每当我们想要将输入的值传递到多个界面时,只是使用Intent传值的话,就会有一些的弊端. 下面我就以三个页面为例,进行简单的说明一下: 思路: 1.第一个页面是客户输入相关的信息. 2.将客户输入的信 ...

  8. Android开发之旅: Intents和Intent Filters(理论部分)

    引言 大部分移动设备平台上的应用程序都运行在他们自己的沙盒中.他们彼此之间互相隔离,并且严格限制应用程序与硬件和原始组件之间的交互. 我们知道交流是多么的重要,作为一个孤岛没有交流的东西,一定毫无意义 ...

  9. 【Android UI】Android开发之View的几种布局方式及实践

    引言 通过前面两篇: Android 开发之旅:又见Hello World! Android 开发之旅:深入分析布局文件&又是“Hello World!” 我们对Android应用程序运行原理 ...

  10. Android开发之TextView高级应用

    Android开发之TextView高级应用 我们平时使用TextView往往让它作为一个显示文字的容器,但TextView的功能并不局限于此.以下就和大家分享一下TextView的一些使用技巧. A ...

随机推荐

  1. dubbo_分布式Rpc服务

    dubbo是一个分布式的服务架构,可直接用于生产环境作为SOA服务或Rpc服务 1.下载,编译,运行demo  1).安装zookeeper    下载:http://apache.claz.org/ ...

  2. Angularjs学习笔记3_datepicker

    1.使用jquery-ui +angular      <script src="http://libs.useso.com/js/jquery/1.11.1/jquery.js&qu ...

  3. 李洪强iOS开发之大神必备的Xcode插件

    iOS开发大神必备的Xcode插件 写在前面 工欲善其事,必先利其器,iOS开发中不仅要学会Xcode的基本操作,而且还得学会一些Xcode的使用技巧,如掌握常用的快捷键等,还有就是今天要说到的Xco ...

  4. unity free asset

    Unity Test Tools https://www.assetstore.unity3d.com/#/content/13802 Sample Assets (beta) https://www ...

  5. 使用nmap 验证多种漏洞

    0x00 前言 大家都知道在平时的漏扫中,AWVS.APPSCAN.Netspark.webspectort等漏扫工具扫描出来的漏洞问题往往存在误报,这时我们就需要进行人工手动验证漏洞,这里我们有两种 ...

  6. 集合Map映射(使用xml文件)

    Hibernate允许我们将Map元素与RDBMS进行映射. 我们知道,List和Map是基于索引的集合. 在map的情况下,索引列作为键,元素列用作值. 使用xml文件在集合映射中映射Map的示例 ...

  7. ASP.NET动态网站制作(25)-- ADO.NET(4)

    前言:这节课老师主要讲网页当中内容的分页效果,自己写一个分页控件. 内容: 1.首先写出HTML代码: <div id="pager"> <%=GetPagerH ...

  8. 可以这样创建E-Notebook数据库

    最新版的ChemDraw的名称是ChemOffice Professional 15.用户朋友们在使用它的E-Notebook功能的时候,会发现需要先创立一个数据库,作为上传文档的储存空间.并且还可以 ...

  9. day5笔记

    一.上节回顾: 1,find通过元素找索引,可切片,找不到返回-12,index,找不到报错.3,split 由字符串分割成列表,默认按空格.4,captalize 首字母大写,其他字母小写.5,up ...

  10. mysql 高级语法手记

    Select字段时: CASE  WHEN  type=1  THEN  1  ELSE  0  END  as  type1 sum和count同样可以使用case then 时间戳转时间: FRO ...