Launcher源码中Intent.createChooser()实现原理及小例子
2013-07-31 14:46
453 查看
在上一篇中,简单的分析了一下源代码,在
[java] view
plaincopy
final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Intent chooser = Intent.createChooser(pickWallpaper,
getText(R.string.chooser_wallpaper));
处百思不得其解,后来在网上找,也没有很透彻的解释。先看下它的官方文档吧:
[java] view
plaincopy
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都有
[html] view
plaincopy
<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这里面的解释,我英语不是太好,按照我自己的理解就是,你如果像下面这样
[html] view
plaincopy
Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Intent chooser = Intent.createChooser(pickWallpaper,
建立一个intent chooser,系统会寻找所有activity,然后把有
[html] view
plaincopy
<intent-filter>
<action android:name="android.intent.action.SET_WALLPAPER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
定义的activity形成列表提供给使用者。为了验证我的想法,个人写了一个很简单的小例子,MainActivity代码如下:
[java] view
plaincopy
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,代码如下
[java] view
plaincopy
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文件:
[java] view
plaincopy
<?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>
[java] view
plaincopy
<activity android:name=".Demo">
[java] view
plaincopy
<intent-filter>
[java] view
plaincopy
<action android:name="android.intent.action.SET_WALLPAPER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
/manifest>
注意:
[html] view
plaincopy
</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中给大家看的图片,为什么我的选项多了一个。说到这里,想必大家都明白了这个原理了,中秋节还有几分钟就要到了,祝福大家中秋愉快。
上面所说的简单的小例子下载地址:http://download.csdn.net/detail/aomandeshangxiao/3593740
[java] view
plaincopy
final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Intent chooser = Intent.createChooser(pickWallpaper,
getText(R.string.chooser_wallpaper));
处百思不得其解,后来在网上找,也没有很透彻的解释。先看下它的官方文档吧:
[java] view
plaincopy
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都有
[html] view
plaincopy
<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这里面的解释,我英语不是太好,按照我自己的理解就是,你如果像下面这样
[html] view
plaincopy
Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Intent chooser = Intent.createChooser(pickWallpaper,
建立一个intent chooser,系统会寻找所有activity,然后把有
[html] view
plaincopy
<intent-filter>
<action android:name="android.intent.action.SET_WALLPAPER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
定义的activity形成列表提供给使用者。为了验证我的想法,个人写了一个很简单的小例子,MainActivity代码如下:
[java] view
plaincopy
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,代码如下
[java] view
plaincopy
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文件:
[java] view
plaincopy
<?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>
[java] view
plaincopy
<activity android:name=".Demo">
[java] view
plaincopy
<intent-filter>
[java] view
plaincopy
<action android:name="android.intent.action.SET_WALLPAPER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
/manifest>
注意:
[html] view
plaincopy
</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中给大家看的图片,为什么我的选项多了一个。说到这里,想必大家都明白了这个原理了,中秋节还有几分钟就要到了,祝福大家中秋愉快。
上面所说的简单的小例子下载地址:http://download.csdn.net/detail/aomandeshangxiao/3593740
相关文章推荐
- Launcher源码中Intent.createChooser()实现原理及小例子
- Launcher源码中Intent.createChooser()实现原理及小例子
- Android源码解析:Intent实现原理
- spring源码学习之路---IOC实现原理(三)
- Base64编码的原理及实现(源码)
- Intent的用法以及原理实现
- [转]网站JS源码1--产品分类下拉菜单实现原理
- HashSet实现不重复储值原理-附源码解析
- create-react-app路由的实现原理
- (转载)Java中HashMap底层实现原理(JDK1.8)源码分析
- Android 从源码的角度解析Scroller的滚动实现原理
- 【Android应用开发】-(19)Android 串口编程原理和实现方式(附源码)
- redis事务实现原理(源码分析)【转】
- wp7 不重启程序之动态换肤实现原理以及源码
- com编程基础和QueryInterface函数的实现原理例子
- Android 带你从源码的角度解析Scroller的滚动实现原理
- java中使用线程实现Timer(定时器)原理和源码
- 神经网络中 BP 算法的原理与 Python 实现源码解析
- IoC容器Autofac(3) - 理解Autofac原理,我实现的部分Autofac功能(附源码)
- Android开发知识点:利用意图行为选择器Intent.ACTION_CHOOSER实现“分享更多”功能