问题描述
我有一个功能,我只希望仅在某些条件得到满足,所以我有这个活动别名:
I have a feature that I only want to be available only when certain conditions are met so I have this activity alias:
<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>
在活动中我有这个code,使组件
Once in activity I have this code to enable the component
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);
}
现在的问题是它崩溃的说法布拉布拉包不包含名为共享文件文本的活动,怎么可以禁用/启用此元素起诉的别名?谢谢
The problem is it crashes saying the package blabla doesn't contain an activity named share-files-text, how can disable/enable this element suing the alias name?Thanks
编辑:我得到了这个职位的想法:Android:我可以启用/禁用编程活动的意图过滤器?
I got the idea from this post:Android: Can I enable/disable an activity's intent filter programmatically?
推荐答案
我发现用这个code溶液
I have found the solution using this code
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();
}
如果你的别名是别名 组件名componentWithoutTextFiles =新的组件名(的packageName,软件包名.alias);
if your alias is "alias" ComponentName componentWithoutTextFiles = new ComponentName(packageName, packageName".alias");
这篇关于Android的:如何使用&LT在code创建组件名;以活动为别名&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!