展会信息港展会大全

Android开发之Intent.createChooser() 妙用
来源:互联网   发布日期:2016-01-14 09:15:17   浏览:1642次  

导读:1Intent.createChooser(ntent target, CharSequence title)其实 大家对该功能第一影响就是ApiDemo 里面的 其只有区区几行代码 提取为:1Intent intent =newIntent(Intent.ACTION_GET_CONTENT);2intent.......

1

Intent.createChooser(ntent target, CharSequence title)

其实 大家对该功能第一影响就是ApiDemo 里面的 其只有区区几行代码提取为:

1

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

2

intent.setType("audio/*");

3

startActivity(Intent.createChooser(intent, "Select music"));

执行之 会弹出一个对话框 效果为:

其实 对于这段代码 大家应该都能猜出什么意思现自己模拟并理解之

[代码]

1. 定义TestActivity 用于根据传入Uri播放目标

01

public class TestActivity extends Activity {

02

03

@Override

04

public void onCreate(Bundle savedInstanceState) {

05

super.onCreate(savedInstanceState);

06

setContentView(R.layout.main);

07

this.setTitle("TestActivity");

08

09

Intent i = this.getIntent();

10

11

Uri u = i.getData();

12

13

try {

14

playMusic(u);

15

} catch (IllegalArgumentException e) {

16

// TODO Auto-generated catch block

17

e.printStackTrace();

18

} catch (SecurityException e) {

19

// TODO Auto-generated catch block

20

e.printStackTrace();

21

} catch (IllegalStateException e) {

22

// TODO Auto-generated catch block

23

e.printStackTrace();

24

} catch (IOException e) {

25

// TODO Auto-generated catch block

26

e.printStackTrace();

27

}

28

}

29

30

public void playMusic(Uri uri) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException{

31

MediaPlayer mp = new MediaPlayer();

32

mp.setDataSource(this, uri);

33

mp.prepare();

34

mp.start();

35

}

36

}

2. 在AndroidManifest 注册TestActivity

1

<activity android:name=".TestActivity" android:label="TestActivity">

2

<intent-filter>

3

<action android:name="android.intent.action.GET_CONTENT" />

4

<category android:name="android.intent.category.DEFAULT" />

5

<category android:name="android.intent.category.OPENABLE" />

6

<data android:mimeType="audio/music1" />

7

</intent-filter>

8

</activity>

3. 使用TestActivity

1

public void sendChooser(){

2

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

3

intent.setDataAndType(Uri.parse("file:///sdcard/DCIM/cc.mp3"),"audio/music1");

4

startActivity(Intent.createChooser(intent, "Select music1 app"));

5

}

4. emulator 运行截图:

赞助本站

人工智能实验室
AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港