我看到别人的代码怎么Intent的setPackage是写自己的呢?我一直错了?
好多人都是Intent.setPackage(自己的包名)
我一直是认为是目标的包名,我错了很多年?
于是我打开里面的说明看看
/**
* (Usually optional) Set an explicit application package name that limits
* the components this Intent will resolve to. If left to the default
* value of null, all components in all applications will considered.
* If non-null, the Intent can only match the components in the given
* application package.
*
* @param packageName The name of the application package to handle the
* intent, or null to allow any application package.
*
* @return Returns the same Intent object, for chaining multiple calls
* into a single statement.
*
* @see #getPackage
* @see #resolveActivity
*/
public @NonNull Intent setPackage(@Nullable String packageName) {
if (packageName != null && mSelector != null) {
throw new IllegalArgumentException(
"Can't set package name when selector is already set");
}
mPackage = packageName;
return this;
}
大概翻译一下:
通常是可选的,设置一个目标应用的包名来限制意图的目标,如果使用默认值空,所有的应用组件都会被匹配,如果不为空,意图只会匹配所给包名的应用组件。
@param packageName The name of the application package to handle the
intent, or null to allow any application package.
packageName 包名,处理intent的应应用包名,可以填写空(不填写)
所以呢,这个包名呀,应该是填写对方的包名。
我看了好多个以前的人写的项目,好多都写自己的包名。我都怀疑我学的是假的android。