1

Android で独自のランチャーを作成しようとしています。Androidサンプル Homeを使用しました。

サンプルは実際には簡単ではなく、それに関するドキュメントやチュートリアルはほとんどなく、明確な回答のないフォーラムでのいくつかの質問だけです。

お気に入りのアプリケーションをランチャーに追加しようとしましたが、アプリケーションがetc/favorites.xml存在しない xml ファイル " " を検索しています。

このファイルをプログラムで作成する必要がありますか? そして、このファイルはどのように見えますか?

4

1 に答える 1

1

解決策を見つけました。

「assets」フォルダーに「favorites.xml」ファイルを作成し、次のように記述します。

<?xml version="1.0" encoding="UTF-8"?>

<favorites>
    <favorite package="com.android.email" class="com.android.email.activity.Welcome"/>
    <favorite package="com.android.browser" class="com.android.browser.BrowserActivity"/>
</favorites>

サンプル コードでは、メソッド「bindFavorites」を編集しました (FileReader の代わりに InputStream を使用して xml ファイルを読み込みます)。

/**
 * Refreshes the favorite applications stacked over the all apps button.
 * The number of favorites depends on the user.
 */
private void bindFavorites(boolean isLaunching) {
    if (!isLaunching || mFavorites == null) {

        if (mFavorites == null) {
            mFavorites = new LinkedList<ApplicationInfo>();
        } else {
            mFavorites.clear();
        }          

        InputStream is = null;
        try {
            is = getAssets().open("favorites.xml");
        } catch (IOException e) {
            Log.e(LOG_TAG, "Couldn't find or open favorites file ");
            return;
        }

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

        final PackageManager packageManager = getPackageManager();

        try {
            final XmlPullParser parser = Xml.newPullParser();
            parser.setInput(is, "UTF-8");

            beginDocument(parser, TAG_FAVORITES);

            ApplicationInfo application;

            while (true) {
                nextElement(parser);
                String name = parser.getName();
                if (!TAG_FAVORITE.equals(name)) {
                    break;
                }

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

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

                application = getApplicationInfo(packageManager, intent);
                if (application != null) {
                    application.intent = intent;
                    mFavorites.addFirst(application);
                }
            }
        } catch (XmlPullParserException e) {
            Log.w(LOG_TAG, "Got exception parsing favorites.", e);
        } catch (IOException e) {
            Log.w(LOG_TAG, "Got exception parsing favorites.", e);
        }
    }

    mApplicationsStack.setFavorites(mFavorites);
}

それは機能しますが、まだ助けが必要です.xmlファイルでクラス値を設定する必要があり、この情報がどこにあるのかわかりません。ご覧のとおり、この値はアプリケーションによって異なります。ここでいくつかの値を見つけました: http://forum.xda-developers.com/showthread.php?t=836719 しかし、私は独自のチタンアプリケーションを持っており、どのクラス値が必要かわかりません。

于 2012-11-15T16:04:49.553 に答える