android Intent.createChooser 应用选择器 !!!

参考:http://www.iteye.com/topic/690376


http://www.cnblogs.com/wservices/archive/2010/07/07/1772737.html

http://hi.baidu.com/lck0502/blog/item/2d0036efb844a800fcfa3c58.html


也许 下面的场景 我们经常碰到:



但是 你可知道原因 :

Java代码   收藏代码
  1. Intent intent = new Intent(Intent.ACTION_GET_CONTENT);  
  2. intent.setType("audio/*");  
  3. startActivity(Intent.createChooser(intent, "Select music")); 

它使用了Intent.ACTION_GET_CONTENT 和 MIME 类型来查找支持audio/* 的所有Data Picker ,允许用户选择其中之一。

我们也可以让自己的activity出现在picker里面 :

Java代码   收藏代码
  1. <activity android:name=".TestActivity"  
  2.                   android:label="TestActivity">  
  3.             <intent-filter>  
  4.                 <action android:name="android.intent.action.GET_CONTENT" />  
  5.                  <category android:name="android.intent.category.DEFAULT" />  
  6.                  <category android:name="android.intent.category.OPENABLE" />  
  7.                  <data android:mimeType="audio/music1" />  
  8.             </intent-filter>  
  9.         </activity>  
<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>



Intent.ACTION_CHOOSER = “android.intent.action.CHOOSER” 其作用是显示一个Activity选择器。

Intent提供了一个静态的createChooser方法,让我们能够更方便的创建这样一个Intent

我们再看一个设置壁纸的例子
Java代码   收藏代码
  1. private void startWallpaper() {  
  2.   
  3.         closeAllApps(true);  
  4.   
  5.         final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);  
  6.   
  7.         Intent chooser = Intent.createChooser(pickWallpaper,  
  8.   
  9.                 getText(R.string.chooser_wallpaper));  
  10.   
  11.    
  12.   
  13.         startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);  
  14.   
  15.     }  
private void startWallpaper() {

        closeAllApps(true);

        final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);

        Intent chooser = Intent.createChooser(pickWallpaper,

                getText(R.string.chooser_wallpaper));

 

        startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);

    }



在Home界面“按Menu键”--“点击壁纸”后自然就能在一个对话框里列出这些应用,让用户选择到哪里去设置壁纸了,如下图所示。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值