从TargetActivity中提取/区分ActivityAlias名称 [英] Extract / distinguish ActivityAlias name from TargetActivity

查看:106
本文介绍了从TargetActivity中提取/区分ActivityAlias名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个ActivityAliases,它们都以一个TargetActivity开头. 在我的TargetActivity内部,我尝试区分开始的别名 TargetActivity.

I have multiple ActivityAliases which all start one single TargetActivity. Inside of my TargetActivity I try to distinguish the alias that started the TargetActivity.

别名在Manifest中定义为可启动项(意图过滤器),将显示为 主屏幕上的快捷方式.用户将点击快捷方式和android 将开始活动,该活动是我在"android:targetActivity=.."标记中定义的.

The aliases are defined in Manifest as launchables (intent-filter), which will be displayed as shortcuts on the homescreen. The user will click on the shortcut and android will start the activity, which I defined in the "android:targetActivity=.." tag.

为此,我目前从Intent中提取componentName,这是 给我的TargetActivity并使用className().

For that I currently extract the componentName from the Intent, which is given to my TargetActivity and using the className().

赞:

String aliasName = targetActivity.getComponentName().getClassName();

这在许多设备上都可以正常工作.但目前我看到了一些失败 OnePlus .在该设备上,我的技术仅返回 我的TargetActivity ,因此我无法执行该操作,具体取决于 用户启动的别名.

This works fine for a lot of devices. But currently I see some failures on the OnePlus. At that device, my technique only returns the className of my TargetActivity and therefore I can't deliver the action, based on the alias that the user started.

还有其他任何可靠方法来获取用于ActivityAlias的方法 启动TargetActivity?只要名称本身不必长 因为我可以区分它们. 我不想为每个别名创建专用的TargetActivities!

Are there any other, reliable methods to get ActivityAlias that was used to start the TargetActivity? It does not need to be the name itself, as long as I can distinguish them. I do not want to create dedicated TargetActivities for every Alias!

谢谢!

Ps .:我看到了另一种方法,它使用PackageManager来检索 activityInfo并使用Activity.name.但是我怀疑这会不会 返回与我的第一种方法不同的东西.

Ps.: I saw another way, which uses the PackageManager to retrieve the activityInfo and using the Activity.name. But I doubt that this will return something different than my first approach.

ActivityInfo aInfo = activity.getPackageManager().getActivityInfo(activity.getComponentName(), PackageManager.GET_META_DATA);

String aliasName = aInfo.name;

我还尝试了元数据:

不幸的是,该方法不适用于所有设备. 如果从activity.component解析的alias-className返回 target-activity,那么元数据方法也会失败.

Unfortunately this method does not work for all devices. If the parsed alias-className from the activity.component returns the target-activity, then the meta-data approach fails, too.

我继续寻找答案,并找到了一个很好的解决方法 在Tar​​getActivty内部区分我的别名.

I continued searching for an answer and found a good workaround to distinguish between my aliases inside of the TargetActivty.

您可以像这样在ActivityAlias标记中提供元数据:

You can provide meta-data within your ActivityAlias tag like so:

<activity-alias 
    android:name=".aliasName" 
    android:enabled="false" 
    android:exported="true" 
    android:icon="@drawable/someIconRes" 
    android:label="@string/someLabelRes" android:targetActivity=".yourTargetActivity"> 
    <meta-data android:name="alias" android:value="valueToDistinguish"/>
    <intent-filter>  
        <action android:name="android.intent.action.MAIN"/> 
        <category android:name="android.intent.category.LAUNCHER"/> 
    </intent-filter> 
</activity-alias>

这里的重要部分是:

<meta-data android:name="alias" android:value="valueToDistinguish"/>

<activity-alias>此处</activity-alias>标记内.

要提取元数据,您可以从PackageManager获取ActivityInfo 使用TargetActivity:

To extract the meta-data you can get an ActivityInfo from the PackageManager with the TargetActivity:

ActivityInfo appInfo = activity.getPackageManager().getActivityInfo(activity.getComponentName(), PackageManager.GET_META_DATA);

并通过以下方式提取元数据:

And extract the meta-data via:

    private static String getAliasNameByMetaData(ActivityInfo app) {
    String aliasName = "";
    if (app.metaData != null && app.metaData.containsKey(ALIAS_META_DATA_KEY)) {
        aliasName = app.metaData.getString(ALIAS_META_DATA_KEY, "");
    } else {
        LOG.i("AliasName by META-DATA didn't work!");
    }
    LOG.v("AliasName by META-DATA: " + aliasName);
    return aliasName;
}

使用调试器检查Activity类,它包含一个mActivityInfo字段,该字段与getPackageManager().getActivityInfo()返回的ActivityInfo不同,因此您可以使用反射提取它并检查其名称.

Inspecting the Activity class using the debugger, it includes a mActivityInfo field that is different from the ActivityInfo returned by getPackageManager().getActivityInfo() so you can extract it using reflection and check it's name.

注意:看来getPackageManager().getActivityInfo()返回的ActivityInfoActivitymActivityInfo的浅表副本.因此,也许此方法不能解决问题:

Note: It seems that the ActivityInfo returned by getPackageManager().getActivityInfo() is a shallow copy of the mActivityInfo from the Activity. So, maybe this method does not resolve the issue:

Field field = Activity.class.getDeclaredField("mActivityInfo");
field.setAccessible(true);
ActivityInfo value = (ActivityInfo) field.get(this);
Log.e("APPINFO2", "NAME: " + value.name);

推荐答案

1.元数据

不幸的是,该方法不适用于所有设备. 如果从activity.component解析的alias-className返回 target-activity,那么元数据方法也会失败.

Unfortunately this method does not work for all devices. If the parsed alias-className from the activity.component returns the target-activity, then the meta-data approach fails, too.

我继续寻找答案,并找到了一个很好的解决方法 在Tar​​getActivty内部区分我的别名.

I continued searching for an answer and found a good workaround to distinguish between my aliases inside of the TargetActivty.

您可以在ActivityAlias标记内提供元数据,如下所示:

You can provide meta-data within your ActivityAlias tag like so:

<activity-alias 
    android:name=".aliasName" 
    android:enabled="false" 
    android:exported="true" 
    android:icon="@drawable/someIconRes" 
    android:label="@string/someLabelRes" android:targetActivity=".yourTargetActivity"> 
    <meta-data android:name="alias" android:value="valueToDistinguish"/>
    <intent-filter>  
        <action android:name="android.intent.action.MAIN"/> 
        <category android:name="android.intent.category.LAUNCHER"/> 
    </intent-filter> 
</activity-alias>

这里的重要部分是:

<meta-data android:name="alias" android:value="valueToDistinguish"/>

<activity-alias>此处</activity-alias>标记内.

要提取元数据,您可以从PackageManager获取ActivityInfo 使用TargetActivity:

To extract the meta-data you can get an ActivityInfo from the PackageManager with the TargetActivity:

ActivityInfo appInfo = activity.getPackageManager().getActivityInfo(activity.getComponentName(), PackageManager.GET_META_DATA);

并通过以下方式提取元数据:

And extract the meta-data via:

    private static String getAliasNameByMetaData(ActivityInfo app) {
    String aliasName = "";
    if (app.metaData != null && app.metaData.containsKey(ALIAS_META_DATA_KEY)) {
        aliasName = app.metaData.getString(ALIAS_META_DATA_KEY, "");
    } else {
        LOG.i("AliasName by META-DATA didn't work!");
    }
    LOG.v("AliasName by META-DATA: " + aliasName);
    return aliasName;
}


2. mActivityInfo

使用调试器检查Activity类,它包含一个mActivityInfo字段,该字段与getPackageManager().getActivityInfo()返回的ActivityInfo不同,因此您可以使用反射提取它并检查其名称.

Inspecting the Activity class using the debugger, it includes a mActivityInfo field that is different from the ActivityInfo returned by getPackageManager().getActivityInfo() so you can extract it using reflection and check it's name.

注意:看来getPackageManager().getActivityInfo()返回的ActivityInfoActivitymActivityInfo的浅表副本.因此,也许此方法不能解决问题:

Note: It seems that the ActivityInfo returned by getPackageManager().getActivityInfo() is a shallow copy of the mActivityInfo from the Activity. So, maybe this method does not resolve the issue:

Field field = Activity.class.getDeclaredField("mActivityInfo");
field.setAccessible(true);
ActivityInfo value = (ActivityInfo) field.get(this);
Log.e("APPINFO2", "NAME: " + value.name);

这篇关于从TargetActivity中提取/区分ActivityAlias名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆