android - 在安卓中使用 AliasActivity

标签 android xml

根据 documentation AliasActivity stub 可用于选择要启动的 Activity 。 AliasActivity 只是普通的 Activity ,它所做的只是解析 android.app.alias 标签以使用 xml 文件启动 Activity 。在 android list 文件中,它可以像这样使用:

<activity
    android:name="android.app.AliasActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

    <meta-data
        android:name="android.app.alias"
        android:resource="@xml/alias" />
</activity>

搜索后我发现 alias.xml 可能如下所示:

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

<alias xmlns:android="http://schemas.android.com/apk/res/android">
    <intent
        android:targetPackage="com.example.aliasexample"
        android:targetClass="com.example.aliasexample.MainActivity"
        android:data="http://www.google-shmoogle.com/">
    </intent>

</alias>

那么如何使用AliasActivity。我不只是在寻找解决方案,我还想找到优雅的解决方案,并尝试将其用于 AliasActivity。例如,如果设置了某些共享首选项,我应该启动一项 Activity ,如果未设置,则应启动另一项 Activity 。是否可以为此使用 AliasActivity,或者它可以仅用于我们在 xml 中指定的某些特定于设备的特征?那么使用AliasActivity的原因是什么?

第二个问题是我可以直接在 xml 中访问某些 sharedPreference 值吗?

最佳答案

  1. 要根据 SharedPreference 值打开不同的 Activity ,您可以使用常规的 Activity 类。在 onCreate() 内部,只需检查所需的值并从那里开始另一个 Activity ,然后在第一个 Activity 中调用 finish()。看起来好像第一个 Activity 不存在。

  2. 别名 Activity 标签用于定义多个启动器 Activity 。根据您的 list ,您会看到应用的多个应用图标。 Example Here.

编辑 1:除一处外,在任何地方都找不到 AliasActivity 的实现。添加巨大的源代码。不言自明。

/**
 * Stub activity that launches another activity (and then finishes itself)
 * based on information in its component's manifest meta-data.  This is a
 * simple way to implement an alias-like mechanism.
 * 
 * To use this activity, you should include in the manifest for the associated
 * component an entry named "android.app.alias".  It is a reference to an XML
 * resource describing an intent that launches the real application.
 */
public class AliasActivity extends Activity {
    /**
     * This is the name under which you should store in your component the
     * meta-data information about the alias.  It is a reference to an XML
     * resource describing an intent that launches the real application.
     * {@hide}
     */
    public final String ALIAS_META_DATA = "android.app.alias";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        XmlResourceParser parser = null;
        try {
            ActivityInfo ai = getPackageManager().getActivityInfo(
                    getComponentName(), PackageManager.GET_META_DATA);
            parser = ai.loadXmlMetaData(getPackageManager(),
                    ALIAS_META_DATA);
            if (parser == null) {
                throw new RuntimeException("Alias requires a meta-data field "
                        + ALIAS_META_DATA);
            }

            Intent intent = parseAlias(parser);
            if (intent == null) {
                throw new RuntimeException(
                        "No <intent> tag found in alias description");
            }

            startActivity(intent);
            finish();

        } catch (PackageManager.NameNotFoundException e) {
            throw new RuntimeException("Error parsing alias", e);
        } catch (XmlPullParserException e) {
            throw new RuntimeException("Error parsing alias", e);
        } catch (IOException e) {
            throw new RuntimeException("Error parsing alias", e);
        } finally {
            if (parser != null) parser.close();
        }
    }

    private Intent parseAlias(XmlPullParser parser)
            throws XmlPullParserException, IOException {
        AttributeSet attrs = Xml.asAttributeSet(parser);

        Intent intent = null;

        int type;
        while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
                && type != XmlPullParser.START_TAG) {
        }

        String nodeName = parser.getName();
        if (!"alias".equals(nodeName)) {
            throw new RuntimeException(
                    "Alias meta-data must start with <alias> tag; found"
                    + nodeName + " at " + parser.getPositionDescription());
        }

        int outerDepth = parser.getDepth();
        while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
               && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
            if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
                continue;
            }

            nodeName = parser.getName();
            if ("intent".equals(nodeName)) {
                Intent gotIntent = Intent.parseIntent(getResources(), parser, attrs);
                if (intent == null) intent = gotIntent;
            } else {
                XmlUtils.skipCurrentTag(parser);
            }
        }

        return intent;
    }

}

关于android - 在安卓中使用 AliasActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24324919/

相关文章:

java - 带有删除和添加通知的自定义数组列表

java - 坚持使用 JAXB - 解码 XML 异常

php "glob"模式帮助

java - SAX 解析器不工作

android - Activity 等待 Intent

Android Oreo - 如何在 Cordova 中设置自适应图标?

c# - 仅在 C# 中验证 XML 语法

java - 如何查看Word标题中的空间量?

android - DocumentContract 上的 ContentResolver 查询列出所有文件,忽略选择

android - 发送不 protected 广播 com.motorola.motocare.INTENT_TRIGGER java.lang.Throwable