在android中使用AliasActivity

时间:2014-06-20 10:13:33

标签: android xml

根据documentation AliasActivity存根可用于选择要启动的活动。 AliasActivity只是普通活动,它只是解析android.app.alias标记来使用xml文件来启动活动。在android清单文件中,它可以像:

一样使用
<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。例如,我应该在某个共享首选项集的情况下启动一个活动,如果没有设置则启动另一个活动。是否可以使用AliasActivity,或者它可以仅用于我们在xml中指定的某些特定于设备的特性?那么使用AliasActivity的原因是什么?

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

1 个答案:

答案 0 :(得分:0)

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

  2. 别名活动标记用于定义多个启动器活动。您会看到应用的多个应用图标,具体取决于您的清单。 Example Here.

  3. 编辑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;
        }
    
    }