Robert Tupelo-Schneck 的回答是关于优先选择对象而不是字符串是正确的。这是我的看法。
-
要引用您自己的组件,请使用:
new ComponentName(getApplicationContext(), WidgetProvider.class);
-
要在您自己的应用程序中引用某些动态引用的组件,请使用:
// values/strings.xml: <string name="provider">de.zordid.sampleapp.widget.WidgetProvider</string>
String fqcn = getResources().getString(R.string.provider);
new ComponentName(getApplicationContext(), fqcn);
当您想使用 Android 的资源限定符来决定使用哪个组件时,这很有用,您可以覆盖 values-*/strings.xml 中的默认字符串。
-
要引用另一个应用程序的组件,请使用:
int componentFlags = GET_ACTIVITIES | GET_PROVIDERS | GET_RECEIVERS | GET_SERVICES;
PackageInfo otherApp = context.getPackageManager().getPackageInfo("com.other.app", componentFlags);
ComponentInfo info = otherApp.activities[i]; // or providers/receivers/...
new ComponentName(info.packageName, info.name);
关于.Names 和<manifest package="
这里可能有些混乱,因为我认为从历史上看罗伯特的说法是正确的:
它是应用程序的包名---该应用程序的 AndroidManifest.xml 中清单元素的包属性
但现在没有了。自从引入了新的 Gradle 构建系统以来,some changes 就在这附近。
如果您在build.gradle 中指定了android.defaultConfig.applicationId,这将是应用程序包名称,然后在构建应用程序时,清单中的package 属性是一个单独的东西。 ComponentName 的第一个参数现在引用 applicationId + applicationIdSuffix。棘手的是,在最终清单合并和打包之后,APK 将具有<manifest package=applicationId + applicationIdSuffix,并且所有 .Names 都将扩展为 FQCN。
学习名称解析的示例应用
这是一个基于我的一个应用程序结构的示例结构。考虑一个名为“app”的假设应用程序中的以下类:
net.twisterrob.app.android.App
net.twisterrob.app.android.GlideSetup
net.twisterrob.app.android.subpackage.SearchResultsActivity
net.twisterrob.app.android.subpackage.Activity
net.twisterrob.app.android.content.AppProvider
在应用程序的服务器端后端和/或一些共享模型类:
net.twisterrob.app.data.*
net.twisterrob.app.backend.*
net.twisterrob.app.web.*
在我的 Android 助手库中:
net.twisterrob.android.activity.AboutActivity
其他库:
android.support.v4.content.FileProvider
这样,所有东西都在net.twisterrob.app 中命名。 android 应用只是它自己的子包中整体的一部分。
AndroidManifest.xml(无关部分省略)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.twisterrob.app.android">
<!--
`package` above defines the base package for .Names
to simplify reading/writing the manifest.
Notice that it's different than the `applicationId` in build.gradle
and can be independently changed in case you want to refactor your packages.
This way you can still publish the same app with the same name.
-->
<!-- Will be expanded to net.twisterrob.app.android.App in the manifest merging phase. -->
<application android:name=".App">
<!-- meta-data needs FQCNs because the merger can't know if you want to expand them or not.
Also notice that name and value both can contain class names, depending on what you use. -->
<meta-data android:name="net.twisterrob.app.android.GlideSetup" android:value="GlideModule" />
<meta-data android:name="android.app.default_searchable" android:value="net.twisterrob.app.android.subpackage.SearchResultsActivity" />
<!-- Will be expanded to net.twisterrob.app.android.subpackage.Activity in the manifest merging phase. -->
<activity android:name=".subpackage.Activity" />
<!-- Needs full qualification because it's not under the package defined on manifest element. -->
<activity android:name="net.twisterrob.android.activity.AboutActivity" />
<!-- Will be expanded to net.twisterrob.app.android.content.AppProvider in the manifest merging phase. -->
<provider android:name=".content.AppProvider" android:authorities="${applicationId}" />
<!-- Needs full qualification because it's not under the package defined on manifest element. -->
<provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.share" />
</application>
<!-- ${applicationId} will be replaced with what's defined in `build.gradle` -->
</manifest>
build.gradle
android {
defaultConfig {
// this is what will be used when you upload it to the Play Store
applicationId 'net.twisterrob.app'
}
buildTypes {
debug {
// The neatest trick ever!
// Released application: net.twisterrob.app
// IDE built debug application: net.twisterrob.app.debug
// This will allow you to have your installed released version
// and sideloaded debug application at the same time working independently.
// All the ContentProvider authorities within a system must have a unique name
// so using ${applicationId} as authority will result in having two different content providers.
applicationIdSuffix '.debug'
}
}
}
查看在所有合并打开 build\intermediates\manifests\full\debug\AndroidManifest.xml 之后您的最终清单会是什么样子。