Intent.createChooser 应用选择器

来源:互联网 发布:番茄闹钟软件 编辑:程序博客网 时间:2024/06/05 14:33

原文地址  http://blog.csdn.net/heng615975867/article/details/18983317


简单的分析了一下源代码,在

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

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

[java] view plaincopy
  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.   


在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都有
[html] view plaincopy
  1. <intent-filter>  
  2.                <action android:name="android.intent.action.SET_WALLPAPER" />  
  3.                <category android:name="android.intent.category.DEFAULT" />  
  4.            </intent-filter>  

如下定义,或许你有了些许明白,看下http://groups.google.com/group/android-developers/browse_thread/thread/9d376a94066057a4这里面的解释,我英语不是太好,按照我自己的理解就是,你如果像下面这样
[html] view plaincopy
  1. Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);  
  2.         Intent chooser = Intent.createChooser(pickWallpaper,  
建立一个intent chooser,系统会寻找所有activity,然后把有
[html] view plaincopy
  1. <intent-filter>  
  2.                <action android:name="android.intent.action.SET_WALLPAPER" />  
  3.                <category android:name="android.intent.category.DEFAULT" />  
  4.            </intent-filter>  
定义的activity形成列表提供给使用者。为了验证我的想法,个人写了一个很简单的小例子,MainActivity代码如下:
[java] view plaincopy
  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. }  
还有一个demo,代码如下
[java] view plaincopy
  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. }  

demo.xml文件里面只有一个textview很简单。
然后是AndroidManifest.xml文件:
[java] view plaincopy
  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>  
[java] view plaincopy
  1. <activity android:name=".Demo">  
[java] view plaincopy
  1. <intent-filter>  
[java] view plaincopy
  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>  



注意:
 
[html] view plaincopy
  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>  


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


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

下面是关于一段选择系统已安装的第三方应用分享的片段:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. private void CreateShare(String title) {  
  2.     Intent intent = new Intent(Intent.ACTION_SEND); // 启动分享发送的属性  
  3.     intent.setType("text/plain"); // 分享发送的数据类型  
  4.     intent.putExtra(Intent.EXTRA_TEXT, title); // 分享的内容  
  5.     startActivity(Intent.createChooser(intent,  
  6.             getResources().getString(R.string.choose_to_share)));// 目标应用选择对话框的标题  
  7. }  

搞定了原来腾讯和开心的微薄 
send的activity 的配置为 
<activity android:name=".activity.MicroBlogInput" android:screenOrientation="portrait" android:configChanges="keyboardHidden|orientation" android:windowSoftInputMode="stateAlwaysVisible|adjustResize"> 
            <intent-filter android:label="@string/albums_sendbyWBlog"> 
                <action android:name="android.intent.action.SEND" /> 
                <data android:mimeType="image/*" />
                <category android:name="android.intent.category.DEFAULT" /> 
            </intent-filter> 
        </activity> 
它并没有定义date mimetype=("text/plain"); 
所以intent.setType("text/plain"); 
这个条件就把它过滤掉了 

所以你的代码要把intent.setType("text/plain"); 
这句改为 
intent.setType("imge/*"); 
我测试通过了 不管是模拟器还是实体机 
    也许这个不是最好的办法 但是是解决问题的办法 

解决方法:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public void onClickShare(View view) {    
  2.     
  3.         Intent intent=new Intent(Intent.ACTION_SEND);     
  4.         intent.setType("image/*");     
  5.         intent.putExtra(Intent.EXTRA_SUBJECT, "分享");     
  6.         intent.putExtra(Intent.EXTRA_TEXT, "终于可以了!!!");      
  7.         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);     
  8.         startActivity(Intent.createChooser(intent, getTitle()));     
  9.     
  10.     }    

0 0
原创粉丝点击