- final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
- Intent chooser = Intent.createChooser(pickWallpaper,
- getText(R.string.chooser_wallpaper));
處百思不得其解,後來在網上找,也沒有很透徹的解釋。先看下它的官方文檔吧:
- 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都有
- <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這裏面的解釋,我英語不是太好,按照我自己的理解就是,你如果像下面這樣
- Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
- Intent chooser = Intent.createChooser(pickWallpaper,
- <intent-filter>
- <action android:name="android.intent.action.SET_WALLPAPER" />
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- 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);
- }
- });
- }
- }
- 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文件:
- <?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>
- <activity android:name=".Demo">
- <intent-filter>
- <action android:name="android.intent.action.SET_WALLPAPER" />
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </activity>
- </application>
- /manifest>
注意:
- </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