gpt4 book ai didi

安卓 : how to create componentName in code using

转载 作者:行者123 更新时间:2023-12-02 15:52:21 26 4
gpt4 key购买 nike

我有一个功能,我只想在满足某些条件时才可用,因此我有此 Activity 别名:

<activity-alias android:name="share-files-text"
android:targetActivity=".MyActivity"
android:exported="true"
android:enabled="false">
<intent-filter android:label="@string/open_with">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:mimeType="application/pdf" />
<data android:mimeType="image/*" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity-alias>

一旦进入 Activity ,我就可以使用此代码来启用该组件

        String packageName = getPackageName();
ComponentName componentWithTextFiles = new ComponentName(packageName, "share-files-text");
ComponentName componentWithoutTextFiles = new ComponentName(packageName, "share-files");
if(DebugAndTestSettings.ENABLE_TEXT_SLIDE){
getPackageManager().setComponentEnabledSetting(componentWithTextFiles, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0);
getPackageManager().setComponentEnabledSetting(componentWithoutTextFiles, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);
} else {
getPackageManager().setComponentEnabledSetting(componentWithTextFiles, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);
getPackageManager().setComponentEnabledSetting(componentWithoutTextFiles, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0);
}

问题是它崩溃了,说包 blabla 不包含名为 share-files-text 的 Activity ,如何使用别名禁用/启用此元素?谢谢

编辑:我从这篇文章中得到了这个想法: Android: Can I enable/disable an activity's intent filter programmatically?

最佳答案

我已经使用此代码找到了解决方案

String packageName = getPackageName();
try {
PackageInfo p = getApplicationContext().getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
for (ActivityInfo activityInfo : p.activities) {
if(log.d()) log.d("ACT " + activityInfo.name+" "+activityInfo.packageName);
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}

如果您的别名是“alias”

ComponentName componentWithoutTextFiles = new ComponentName(packageName, packageName".alias");

关于安卓 : how to create componentName in code using <activity-alias>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23919329/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com