关于 RemoteViews 跨进程资源访问的勘误

栏目: IOS · Android · 发布时间: 6年前

内容简介:看到《Android开发艺术探索》书中 P241 页提到RemoteViews跨进程资源访问的限制,我在跨进程的实际使用时,证实是没有限制的,可以跨进程使用,RemoteViews源码如下:RemoteViews实例化的时候会保存一个mApplication的变量,用于跨进程访问资源。感谢网友

看到《Android开发艺术探索》书中 P241 页提到RemoteViews跨进程资源访问的限制,我在跨进程的实际使用时,证实是没有限制的,可以跨进程使用,RemoteViews源码如下:

public RemoteViews(String packageName, int layoutId) {
    this(getApplicationInfo(packageName, UserHandle.myUserId()), layoutId);
}

protected RemoteViews(ApplicationInfo application, int layoutId) {
    mApplication = application;
    ...
}
复制代码

RemoteViews实例化的时候会保存一个mApplication的变量,用于跨进程访问资源。

/** @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 的反馈,希望对大家有所帮助。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

C++ API设计

C++ API设计

[美] Martin Reddy / 刘晓娜、臧秀涛、林健 / 人民邮电出版社 / 2013-8 / 89.00

现代软件开发中的一大难题就是如何编写优质的API。API负责为某个组件提供逻辑接口并隐藏该模块的内部细节。多数程序员依靠的是经验和冒险,从而很难达到健壮、高效、稳定、可扩展性强的要求。Martin Reddy博士在自己多年经验基础之上,对于不同API风格与模式,总结出了API设计的种种最佳策略,着重针对大规模长期开发项目,辅以翔实的代码范例,从而有助于设计决策的成功实施,以及软件项目的健壮性及稳定......一起来看看 《C++ API设计》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具