`
gryphone
  • 浏览: 426885 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

Intent.createChooser() 妙用

阅读更多

Intent.createChooser(ntent target, CharSequence title)

 

 

其实 大家对该功能第一影响就是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 运行截图:

 

 

 

 

 

 

分享到:
评论

相关推荐

    android-将内容分享到自己的app

    像qq,微信那样 调用代码Intent intent=new Intent(Intent.ACTION_SEND); intent.setType("text/plain");... startActivity(Intent.createChooser(intent, "请选择")); 可以选择分享到自己的app

    android Intent实例

    Intent.createChooser(returnIt, "Choose Email Client"); 10,发短信 Uri smsUri = Uri.parse("tel:100861"); returnIt = new Intent(Intent.ACTION_VIEW, smsUri); returnIt.putExtra("sms_body", "shenrenkui"); ...

    android intent使用定义标题

    可以使用 Intent.createChooser() 的方法来创建 Intent,并传入想要的 Sting 作为标题。 以wallpaper 选择框为例,当在Launcher workspace的空白区域上长按,会弹出wallpaper的选择框,选择框的标题为”Choose ...

    android开发使用例子

    startActivity(Intent.createChooser(it, "Choose Email Client")); //搜索应用 Uri uri = Uri.parse("market://search?q=pname:pkg_name"); Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it)...

    wallpaperdemo

    Intent.createChooser() 重点讲诉Intent.createChooser() android中createChooser()实现原理。

    史上最全的 _ Android开发网

    Intent it = new Intent(Intent.ACTION_SEND); ); it.putExtra(Intent.EXTRA_EMAIL, ...it.putExtra(Intent.EXTRA_TEXT, "The email body text");...startActivity(Intent.createChooser(it, "Choose Email Client"));

    inote记事工具android客户端

    Intent.createChooser(intent, "Share"); startActivity(intent); } 5. SQLiteOpenHelper在系统目录创建数据库,如果要把数据库文件保存在SD卡,可通过SQLiteDatabase.openOrCreateDatabase自行实现。 6. SD卡的...

    Android调用系统自带的分享功能实例代码

    实现分享功能的几个办法 1.调用系统的分享功能 2.通过第三方SDK,如ShareSDK,友盟等 3.自行使用各自平台的SDK,比如QQ,微信,微博各自的SDK 这里就记录下第一种办法... startActivity(Intent.createChooser(textIn

    Android中调用系统的文件浏览器及自制简单的文件浏览器

    调用系统自带的文件浏览器 这很简单: /** 调用文件选择软件来选择文件 **/ private void showFileChooser() { ... startActivityForResult(Intent.createChooser(intent, 请选择一个要上传的文件), FILE_SELECT_C

    Android中简单调用图片、视频、音频、录音和拍照的方法

    本文实例讲述了Android中简单调用图片、视频、音频、录音和拍照的方法。分享给大家供大家参考,具体如下: //选择图片 requestCode 返回的标识 Intent innerIntent = ...Intent wrapperIntent = Intent.createChooser

    Android 文件选择的实现代码

    打开文件选择器 代码如下:private void showFileChooser() { Intent intent = new Intent... try { startActivityForResult( Intent.createChooser(intent, “Select a File to Upload”), FILE_SELECT_CODE); 

    简述Android中实现APP文本内容的分享发送与接收方法

    谨记(指定选择器Intent.createChooser()) 开始今天的内容前,先闲聊一下: (1)突然有一天头脑风暴,对很多问题有了新的看法和见解,迫不及待的想要分享给大家,文档已经写好了,我需要通过微信或者QQ,短信等...

Global site tag (gtag.js) - Google Analytics