从不同的应用访问自定义内容提供商

时间:2011-04-19 09:23:50

标签: android android-contentprovider

您好我创建了一个使用名为CustomCP的自定义内容提供程序的Android应用程序, 它实现了所有方法,一切正常,同时管理应用程序内的数据, 但是当我尝试从另一个应用程序访问它时,我不断收到“找不到的错误” com.example.customcp的提供者信息。

我已将第一个应用的清单文件中的内容提供商声明为

<provider android:name="com.example.CustomCP"      android:authorities="com.example.customcp"/>

我尝试在第二个应用程序启动活动中调用提供程序

public class app2 extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Uri kUri = Uri.parse("content://com.example.customcp/key");
        Cursor c = managedQuery(kUri, null, null, null, null);
}
}

所以问题很简单,是否可以从多个应用程序访问自定义内容提供商?

5 个答案:

答案 0 :(得分:14)

是的,可以从其他应用访问自定义内容提供商。使用您的术语,我们将调用内容提供商CustomCP和其他应用AppA。 (AppA是想要访问提供者的人)。事实证明这种方法有效:

  1. 使用ContentProviderClient在AppA中指定所需的内容提供程序(CustomCP):

    Uri yourURI = Uri.parse("content://com.example.customcp/YourDatabase"); ContentProviderClient yourCR = getContentResolver().acquireContentProviderClient(yourURI);

  2. 像往常一样从App A访问内容提供商。例如:

    yourCursor = yourCR.query(yourURI, null, null, null, null);

    注意:您必须将代码包含在try / catch块中,或者包含&#34;抛出RemoteException&#34;因为提供者不在App A中。

  3. CustomCP的清单必须指定提供者,包括允许的权限(例如,读取和/或写入),并且必须导出提供者。这是一个例子:

    <provider
        android:name="your.package.contentprovider.YourProvider"
        android:authorities="YourAuthority"
        android:readPermission="android.permission.permRead"
        android:exported="true" >
     </provider>
    

答案 1 :(得分:0)

在清单文件中,确保您的

"provider android..>"
位于

 "application .. /application>" 

希望有所帮助

答案 2 :(得分:0)

创建内容提供程序后,在清单文件中指定内容提供程序。您可以使用标记提及内容提供商。在提供者标签内部别忘了提及名称和权限属性。这个声明应该是..

<provider
        android:name="pakgName.ProviderClassName"
        android:authorities="pakgName.ProviderClassName"
        android:multiprocess="true" >
    </provider>

在您尝试从提供程序获取数据时,您在authority属性中提到的应该匹配。

答案 3 :(得分:0)

我知道我来晚了,但是以防万一有人偶然发现了这个问题,我认为这是accepted answer。这救了我一天。因此,在这种情况下,我们在一个应用程序中拥有CustomCP。说,另一个应用程序称为AppDemo2。然后首先在CustomCPs AndroidManifest.xml中定义一个权限:

<permission android:name="com.any_string.MY_PERMISSION_FOR_SOMETHING"/>
<application ...
...></application>

接下来,您必须创建一个提供程序:

<application ... >
<provider
            android:authorities="com.package_name_where_customCP_is"
            android:name=".CustomCP"
            android:readPermission="com.any_string.MY_PERMISSION_FOR_SOMETHING"
            android:enabled="true"
            android:exported="true"
            android:multiprocess="true"
            >
        </provider>
<activity...>

最后,在您的第二个应用程序(我的回答为AppDemo2)中,打开其清单文件并添加此权限:

<uses-permission android:name="com.any_string.MY_PERMISSION_FOR_SOMETHING"/>

这是对我有用的答案。我希望这会有所帮助:)

答案 4 :(得分:0)

您可以使用以下代码在其他应用中获取内容提供商数据。 您需要使用内容解析器来获取数据。

try {
    //URL provided in first app while creating the content provider
    val URL = "content://com.test.localstorage.MyContentProvider/authentication"
    val data = Uri.parse(URL)
    val cursor: Cursor? = contentResolver.query(data, null, null, null, "user_id")

    if (cursor != null) {
        if (cursor.moveToFirst()) {
            do {
                return cursor.getString(cursor.getColumnIndex("user_id")).toString()
            } while (cursor.moveToNext())
        }
    }
} catch (ex: Exception) {
    ex.printStackTrace()
}