Android 开发之:Intent.createChooser() 妙用

Intent.createChooser(ntent target, CharSequence title)

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

?
1
2
3
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType( "audio/*" );
startActivity(Intent.createChooser(intent, "Select music" ));

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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

?
1
2
3
4
5
6
7
8
< 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

?
1
2
3
4
5
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 运行截图:

 

 

 

 

在上一篇中,简单的分析了一下源代码,在http://blog.csdn.net/lilu_leo/article/details/6768249

  1. final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);  
  2.         Intent chooser = Intent.createChooser(pickWallpaper,  
  3.                 getText(R.string.chooser_wallpaper));  
final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
        Intent chooser = Intent.createChooser(pickWallpaper,
                getText(R.string.chooser_wallpaper));

处百思不得其解,后来在网上找,也没有很透彻的解释。先看下它的官方文档吧:

  1. public static Intent createChooser (Intent target, CharSequence title)  
  2. Since: API Level 1  
  3.   
  4. Convenience function for creating a ACTION_CHOOSER Intent.  
  5. Parameters  
  6. target  The Intent that the user will be selecting an activity to perform.  
  7. title   Optional title that will be displayed in the chooser.  
  8. Returns  
  9.   
  10.     * Return a new Intent object that you can hand to Context.startActivity() and related methods.   
public static Intent createChooser (Intent target, CharSequence title)
Since: API Level 1

Convenience function for creating a ACTION_CHOOSER Intent.
Parameters
target 	The Intent that the user will be selecting an activity to perform.
title 	Optional title that will be displayed in the chooser.
Returns

    * Return a new Intent object that you can hand to Context.startActivity() and related methods. 



 

在google上面也找了下,慢慢的有些明白,在一篇文章中看到这么一段话:

这里是要找到所有能处理Intent.ACTION_SET_WALLPAPER请求的activity,其字符串表示为android.intent.action.SET_WALLPAPER。使用Eclipse搜索之后,在以下应用的AndroidManifest.xml文件都找到了能处理这个请求的activity:
packages/apps/Gallery
packages/apps/Launcher2
packages/wallpapers/LivePicker
再看看下面的这个图:


壁纸对应的是Launcher2里面的WallpaperChooser.activity。动态壁纸对应的是packages/wallpapers/LivePicker的LiveWallpaperListActivity,他们的共同点 就是在AndroidManifest.xml都有

  1. <intent-filter>  
  2.                <action android:name="android.intent.action.SET_WALLPAPER" />  
  3.                <category android:name="android.intent.category.DEFAULT" />  
  4.            </intent-filter>  
 <intent-filter>
                <action android:name="android.intent.action.SET_WALLPAPER" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>


如下定义,或许你有了些许明白,看下http://groups.google.com/group/android-developers/browse_thread/thread/9d376a94066057a4这里面的解释,我英语不是太好,按照我自己的理解就是,你如果像下面这样

  1. Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);  
  2.         Intent chooser = Intent.createChooser(pickWallpaper,  
Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
        Intent chooser = Intent.createChooser(pickWallpaper,

建立一个intent chooser,系统会寻找所有activity,然后把有

  1. <intent-filter>  
  2.                <action android:name="android.intent.action.SET_WALLPAPER" />  
  3.                <category android:name="android.intent.category.DEFAULT" />  
  4.            </intent-filter>  
 <intent-filter>
                <action android:name="android.intent.action.SET_WALLPAPER" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

定义的activity形成列表提供给使用者。为了验证我的想法,个人写了一个很简单的小例子,MainActivity代码如下:

  1. public class MainActivity extends Activity {  
  2.     /** Called when the activity is first created. */  
  3. private Button button;  
  4.     @Override  
  5.     public void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.main);  
  8.           
  9.         button=(Button)findViewById(R.id.wallpaperButton);  
  10.         button.setOnClickListener(new View.OnClickListener() {  
  11.   
  12. @Override  
  13. public void onClick(View v) {  
  14. // TODO Auto-generated method stub   
  15. final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);  
  16.        Intent chooser = Intent.createChooser(pickWallpaper,"tese the ACTION_SET_WALLPAPER");  
  17.        startActivity(chooser);  
  18. }  
  19. });  
  20.     }  
  21. }  
public class MainActivity extends Activity {
    /** Called when the activity is first created. */
private Button button;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        button=(Button)findViewById(R.id.wallpaperButton);
        button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
       Intent chooser = Intent.createChooser(pickWallpaper,"tese the ACTION_SET_WALLPAPER");
       startActivity(chooser);
}
});
    }
}

还有一个demo,代码如下

  1. public class Demo extends Activity {  
  2.   
  3.   
  4. @Override  
  5. protected void onCreate(Bundle savedInstanceState) {  
  6. // TODO Auto-generated method stub   
  7. super.onCreate(savedInstanceState);  
  8. setContentView(R.layout.demo);  
  9. }  
  10. }  
public class Demo extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);
}
}


demo.xml文件里面只有一个textview很简单。
然后是AndroidManifest.xml文件:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="cn.demo"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.   
  7.   
  8.   
  9.   
  10.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  11.         <activity android:name=".MainActivity"  
  12.                   android:label="@string/app_name">  
  13.             <intent-filter>  
  14.                 <action android:name="android.intent.action.MAIN" />  
  15.                 <category android:name="android.intent.category.LAUNCHER" />  
  16.             </intent-filter>  
  17.         </activity>  
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="cn.demo"
      android:versionCode="1"
      android:versionName="1.0">




    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
  1. <activity android:name=".Demo">  
<activity android:name=".Demo">
  1. <intent-filter>  
      <intent-filter>
  1.             <action android:name="android.intent.action.SET_WALLPAPER" />  
  2.             <category android:name="android.intent.category.DEFAULT" />  
  3.         </intent-filter>  
  4.     </activity>  
  5. </application>  
  6. /manifest>  
                <action android:name="android.intent.action.SET_WALLPAPER" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>
</manifest>




注意:
 

  1. </activity>  
  2. <activity android:name=".Demo">  
  3. <intent-filter>  
  4.                 <action android:name="android.intent.action.SET_WALLPAPER" />  
  5.                 <category android:name="android.intent.category.DEFAULT" />  
  6.             </intent-filter>  
  7.         </activity>  
</activity>
<activity android:name=".Demo">
<intent-filter>
                <action android:name="android.intent.action.SET_WALLPAPER" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>



我在这里面加了intent适配器 
<action android:name="android.intent.action.SET_WALLPAPER" />


运行下程序,点击button按钮,效果如下:




我这个网速太不给力了,弄的心烦意燥,大家看到我自己写的demo在图片中得到了显示,这也是在上一篇 http://blog.csdn.net/aomandeshangxiao/article/details/6767423中给大家看的图片,为什么我的选项多了一个。说到这里

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值