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 ...
随机推荐
- Redis之SkipList数据结构
0.前言 Redis中有序集合zset需要使用skiplist作为存储数据结构, 关于skiplist数据结构描述可以查询wiki, 本文主要介绍Redis实现的skiplist的细节. 1.数据结构 ...
- UML序列图
先准备好之前的类图,然后在最开始的地方新添加一个版块“交互设计” Add Diagram --> Sequence Diagram Add --> Actor建立一个user 然后就可以拖 ...
- 怎样正确写网站title、keywords、description比较标准。
一.title title,也就是标题,是在浏览器上面显示出来的,方便用户了解这个页面的内容;特别是搜索引擎判断你网页内容的主要根据.搜索引擎就很大部分是依靠网站title来判断你网站是关于什么内容的 ...
- Windows下免费软件的首选推荐
PS:以下按装机顺序排列,“|”号后面是备选软件. 启动引导:EasyBCD 虚拟机:VirtualBox Linux:Zorin | Linux Mint(Mate) | Ubuntu 驱动工具:驱 ...
- IOS设计模式浅析之适配器模式(Adapter)
引言 在项目开发中,有时候会遇到这样的一种情景:需要使用以前开发的“一些现存的对象”,但是新环境中要求的接口是这些现存对象所不满足的.怎样应对这种迁移的需求?使得可以复用这些对象,以满足新的应用环境, ...
- UML结构体系简介
一.UML的结构 UML有3种基本的构造块,分别是事物(元素).关系和图.事物是UML中重要的组成部分.关系把事物紧密联系在一起.图是很多有相互相关的事物的组. 二.UML的事物 UML中的事物也称为 ...
- SHELL $RANDOM产生的随机数范围是0到32767
1.使用系统的 $RANDOM 变量 fdipzone@ubuntu:~$ echo $RANDOM 17617 fdipzone@ubuntu:~$ echo $RANDOM 17617 $RAND ...
- POJ 3304 Segments(计算几何:直线与线段相交)
POJ 3304 Segments 大意:给你一些线段,找出一条直线可以穿过全部的线段,相交包含端点. 思路:遍历全部的端点,取两个点形成直线,推断直线是否与全部线段相交,假设存在这种直线,输出Yes ...
- Linux & windows 应用服务器&Oracle数据库服务器备份
Linux篇 tomcat启动加入开机启动项 vi /etc/rc.d/rc.local service iptables stop /usr/local/tomcat/bgyappserv01/ ...
- PYTHON MYSQL 的表创建和插入
import mysql.connector cnx = mysql.connector.connect(user='xx',password='xx++.',host='139.107.11.166 ...