主题:Intent.createChooser() 妙用

使用Intent.createChooser在Android中选择音乐应用
本文介绍如何在Android应用中使用Intent.createChooser功能实现音乐选择器,包括定义TestActivity播放指定Uri的音乐,注册Activity到系统音乐选择器,以及发送选择器意图的完整过程。

Intent.createChooser(ntenttarget,CharSequencetitle)

其实 大家对该功能第一影响就是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")); }

4. emulator 运行截图:

public void button2(View v) throws NSDKException { //读取状态 // 打开相机拍照 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); // 打开摄像机录像 Intent videoIntent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE); // 打开文件选择器(比如图库、文件管理器) Intent pickIntent = new Intent(Intent.ACTION_GET_CONTENT); pickIntent.setType("*/*"); // 创建 Chooser Intent chooser = Intent.createChooser(pickIntent, "选择操作"); // 附加其他可选项 chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{cameraIntent, videoIntent}); // 启动,不关心结果 startActivity(chooser); } 请帮我分析这段代码 为什么1980-02-07 19:01:59.098 5455-5455 StatusBarC...olFragment com.example.demo D Creating file picker intent with type: */* 1980-02-07 19:01:59.098 5455-5455 StatusBarC...olFragment com.example.demo D Creating camera intent for image capture 1980-02-07 19:01:59.098 5455-5455 StatusBarC...olFragment com.example.demo D Creating video intent for video capture 1980-02-07 19:01:59.103 5455-5455 StatusBarC...olFragment com.example.demo D Has camera app: true 1980-02-07 19:01:59.103 5455-5455 StatusBarC...olFragment com.example.demo D Has video app: true 1980-02-07 19:01:59.103 5455-5455 StatusBarC...olFragment com.example.demo D Both camera and video apps are available 1980-02-07 19:01:59.104 5455-5455 StatusBarC...olFragment com.example.demo D Creating chooser with title: 选择操作 1980-02-07 19:01:59.105 5455-5455 StatusBarC...olFr
最新发布
11-14
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值