關於 RemoteViews 跨進程資源訪問的勘誤

看到《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

相關文章
相關標籤/搜索