Android的 - 有供应商权限的应用程序项目 [英] Android - Having Provider authority in the app project

查看:208
本文介绍了Android的 - 有供应商权限的应用程序项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个Android库项目包含了一些供应商,其权威是这样定义的合同类以下内容:

An android library project contains a few providers whose authority is defined like the following in a contract class :

public static final String CONTENT_AUTHORITY = "my.com.library.providers.tester";
private static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);

现在有很多,它使用这个库项目的应用程序项目。我目前遇到的问题是,对于每一个应用程序的项目,我需要有在库项目为每个应用程序单独的分支只是有一个独特的内容授权。这是创造一些版本的管理问题(如传播特性/ bug修复从一个分支到其他每个分支等)。相反,我想委托定义内容授权到应用程序项目的责任。有没有办法做到这一点?

Now there are a lot of app projects which uses this library project. The problem I am currently having is that for every app project, I need to have a separate branch in the library project for every app just for having a unique content authority. This is creating some version management problems (like propagating features/bug fixes from one branch to every other branch etc.,). Instead I would like to delegate the responsibility of defining the content authority to the app project. Is there a way to accomplish this?

推荐答案

该应用程序是唯一一个绝对需要了解的权威,因为它是一个声明<供应商> 在清单中的机器人:当局属性

The app is the only one that absolutely needs to know about the authority, as it is the one that declares the <provider> in the manifest with the android:authorities attribute.

因此​​,在原则上,它应该只是工作,只要你从供应商删除所有权威特定的逻辑,如:

Hence, in principle, it should "just work", so long as you remove all authority-specific logic from the provider, such as:

  • 在这些静态数据成员(现在移动到托管应用程序)
  • UriMatcher (滚动自己的东西,不检查的权力,而是专注于其他部分的乌里
  • those static data members (which now move to the hosting app)
  • UriMatcher (roll something yourself that does not examine the authority, but focuses on the rest of the Uri)

如果由于某种原因,你绝对确保你的供应商需要知道它的权威,那么应用程序必须提供,为供应商之前,提供用于实际工作。可能的方式来做到这一点包括:

If, for some reason, you are absolutely sure that your provider needs to know its authority, then the app will have to supply that to the provider before the provider is used for real work. Possible ways to do that include:

  • 由于的ContentProvider 是一个天然的单,将其分配到一个静态数据成员,然后从一个定制的提供权威的字符串给它一个自定义的方法应用程序类(如供应商首先被初始化,所以这应该工作)

  • Since a ContentProvider is a natural singleton, assign it to a static data member, and then supply the authority string to it by a custom method from a custom Application class (as providers are initialized first, so this should work)

如果你只支持API级别11+,有自定义应用程序类使用通话() ContentResolver的提供权威的的ContentProvider

If you are only supporting API Level 11+, have the custom Application class use call() on ContentResolver to supply the authority to the ContentProvider

假设只有真正的呼叫(例如,查询()插入())都是有效的,只是懒惰初始化根据什么来的第一个乌里你的权威你看

Assume that the only real calls (e.g., to query(), insert()) are valid, and just lazy-initialize your authority based on what comes in on the first Uri you see

这篇关于Android的 - 有供应商权限的应用程序项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆