看到《Android开发艺术探索》书中 P241 页提到RemoteViews跨进程资源访问的限制,我在跨进程的实际使用时,证明是没有限制的,能够跨进程使用,RemoteViews源码以下:bash
public RemoteViews(String packageName, int layoutId) {
this(getApplicationInfo(packageName, UserHandle.myUserId()), layoutId);
}
protected RemoteViews(ApplicationInfo application, int layoutId) {
mApplication = application;
...
}
复制代码
RemoteViews实例化的时候会保存一个mApplication的变量,用于跨进程访问资源。app
/** @hide */
public View apply(Context context, ViewGroup parent, OnClickHandler handler) {
...
final Context contextForResources = getContextForResources(context);
Context inflationContext = new ContextWrapper(context) {
@Override
public Resources getResources() {
return contextForResources.getResources();
}
@Override
public Resources.Theme getTheme() {
return contextForResources.getTheme();
}
@Override
public String getPackageName() {
return contextForResources.getPackageName();
}
};
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
...
}
private Context getContextForResources(Context context) {
if (mApplication != null) {
if (context.getUserId() == UserHandle.getUserId(mApplication.uid)
&& context.getPackageName().equals(mApplication.packageName)) {
return context;
}
try {
return context.createApplicationContext(mApplication,
Context.CONTEXT_RESTRICTED);
} catch (NameNotFoundException e) {
Log.e(LOG_TAG, "Package name " + mApplication.packageName + " not found");
}
}
return context;
}
复制代码
感谢网友 magazmj@gmail.com
的反馈,但愿对你们有所帮助。ide