Home例子研究2

1. 在Home.onCreateOptionsMenu方法中

// 添加三个菜单项 其中系统设置android.provider.Settings.ACTION_SETTINGS

menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper).setIcon(android.R.drawable.ic_menu_gallery)
        .setAlphabeticShortcut('W');
menu.add(0, MENU_SEARCH, 0, R.string.menu_search).setIcon(android.R.drawable.ic_search_category_default)
        .setAlphabeticShortcut(SearchManager.MENU_KEY);
menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings).setIcon(android.R.drawable.ic_menu_preferences)
        .setIntent(new Intent(android.provider.Settings.ACTION_SETTINGS));

2. 在Home.onOptionsItemSelected方法中

item.getItemId() 如果等于MENU_WALLPAPER_SETTINGS 调用方法 startWallpaper 如果等于MENU_SEARCH 调用方法onSearchRequested

3. 在Home.startWallpaper方法中

final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
startActivity(Intent.createChooser(pickWallpaper, getString(R.string.menu_wallpaper)));

4. 程序运行日志 表示bindFavorites方法执行失败

11-03 08:09:05.335: ERROR/Home(6880): Couldn't find or open favorites file /system/etc/favorites.xml

5. 在Home.bindFavorites方法中

// 创建File对象和FileReader对象

// Environment.getRootDirectory() 即 /system 或 ANDROID_ROOT

final File favFile = new File(Environment.getRootDirectory(), DEFAULT_FAVORITES_PATH);

FileReader favReader = new FileReader(favFile);

// 创建intent对象和获取packageManager对象

final Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);

final PackageManager packageManager = getPackageManager();

// 创建XmpPullParser对象 设置输入来自favReader

final XmlPullParser parser = Xml.newPullParser();
parser.setInput(favReader);

// 开始文档解析

beginDocument(parser, TAG_FAVORITES);

// 对于每个元素

nextElement(parser);

// 获取包名和类名

final String favoritePackage = parser.getAttributeValue(null, TAG_PACKAGE);
final String favoriteClass = parser.getAttributeValue(null, TAG_CLASS);

// 创建ComponentName对象 设置intent属性

final ComponentName cn = new ComponentName(favoritePackage, favoriteClass);
intent.setComponent(cn);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

// 获取ApplicationInfo对象

info = getApplicationInfo(packageManager, intent);

// 将ApplicationInfo对象添加到列表中

info.intent = intent;
mFavorites.addFirst(info);

原文地址:https://www.cnblogs.com/fengzhblog/p/2752150.html