ContentResolver(客户端应用程序)无法从内容提供商(服务器应用程序)获取数据

发布于 2025-01-17 18:19:25 字数 3730 浏览 2 评论 0原文

我是安卓开发新手。因此,对于任何简单的问题,请提前接受我的歉意。

我的内容解析器应用程序(客户端应用程序)无法从内容提供程序(服务器应用程序)获取数据。值得一提的是,当我尝试从同一个应用程序测试 ContentProvider 类时,它确实可以工作,但是当我从不同的应用程序测试它时,它返回空光标对象。

Androidmanifest.xml(服务器应用程序)

  <provider
        android:name=".ACProvider"
        android:authorities="com.example.cptest.ACProvider"
        android:exported="true"
        android:readPermission="com.example.cptest.PERMISSION"
        android:writePermission="com.example.cptest.PERMISSION"
        />


  

内容提供程序类:

class ACProvider : ContentProvider() {

companion object {
    val PROVIDER_NAME = "com.example.cptest.ACProvider"
    val URL = "content://" + PROVIDER_NAME + "/ACTABLE"
    val CONTENT_URI = Uri.parse(URL)

    val _ID = "_ID"
    val NAME = "NAME"
    val MEANING = "MEANING"

    const val uriCode = 1

    private val sUriMatcher = UriMatcher(UriMatcher.NO_MATCH).apply {
        addURI(PROVIDER_NAME, "ACTABLE", uriCode)
    }

}

lateinit var db: SQLiteDatabase


override fun onCreate(): Boolean {
    var helper = DBHelper(getContext())
    db = helper.writableDatabase

    return if (db == null) false else true
}

override fun query(
    uri: Uri,
    cols: Array<out String>?,
    condition: String?,
    condition_val: Array<out String>?,
    order: String?
): Cursor? {
    return db.query("ACTABLE", cols, condition, condition_val, null, null, order)
}

override fun getType(p0: Uri): String? {
    return "vnd.android.cursor.dir/vnd.com.example.cptest.ACTABLE"
}

override fun insert(uri: Uri, cv: ContentValues?): Uri? {
    db.insert("ACTABLE", null, cv)
    context?.contentResolver?.notifyChange(uri, null)
    return uri
}

override fun delete(uri: Uri, condition: String?, condition_val: Array<out String>?): Int {
    var count = db.delete("ACTABLE", condition, condition_val)
    context?.contentResolver?.notifyChange(uri, null)
    return count
}

override fun update(
    uri: Uri,
    cv: ContentValues?,
    condition: String?,
    condition_val: Array<out String>?
): Int {
    var count = db.update("ACTABLE", cv, condition, condition_val)
    context?.contentResolver?.notifyChange(uri, null)
    return count
}

}

DBHelperClass:

class DBHelper(context: Context?) : SQLiteOpenHelper(context,"ACDB",null,1){
override fun onCreate(db: SQLiteDatabase?) {
    if (db != null) {
        db.execSQL("CREATE TABLE ACTABLE(_ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, MEANING TEXT)")
        db.execSQL("INSERT INTO ACTABLE (NAME, MEANING) VALUES ('ttt','BACHELOR OF COMPUTER SCIENCE')")
        db.execSQL("INSERT INTO ACTABLE (NAME, MEANING) VALUES ('MCA1','MASTERS OF COMPUTER SCIENCE')")
        db.execSQL("INSERT INTO ACTABLE (NAME, MEANING) VALUES ('BCA','BACHELOR OF COMPUTER SCIENCE')")
    }

    }

override fun onUpgrade(p0: SQLiteDatabase?, p1: Int, p2: Int) {

}

}

Androidmanifest.xml(客户端应用程序)

<uses-permission android:name = "com.example.cptest.PERMISSION"/>

测试代码:

val PROVIDER_NAME = "com.example.cptest.ACProvider"
            val URL = "content://" + PROVIDER_NAME + "/ACTABLE"
            val CONTENT_URI = Uri.parse(URL)

            var rs = contentResolver.query(CONTENT_URI,
                arrayOf("_ID","NAME","MEANING"),null, null, null)

            if (rs != null) {
                if (rs.moveToNext())
                    Toast.makeText(applicationContext, rs.getString(1) + "\n"+ rs.getString(2), Toast.LENGTH_LONG).show()
            }
            else
            {
                Toast.makeText(applicationContext, "No DATA", Toast.LENGTH_LONG).show()
            }

I m new to android development. so please accept my apology in advance for any simple questions.

My Content Resolver app (Client App) can't get the data from Content Provider (Server App). It is worth mentioning that when i try to test the ContentProvider Class from same app it does work but when i test it from different app it returns the null cursor object.

Androidmanifest.xml (Server App)

  <provider
        android:name=".ACProvider"
        android:authorities="com.example.cptest.ACProvider"
        android:exported="true"
        android:readPermission="com.example.cptest.PERMISSION"
        android:writePermission="com.example.cptest.PERMISSION"
        />


  

Content Provider Class :

class ACProvider : ContentProvider() {

companion object {
    val PROVIDER_NAME = "com.example.cptest.ACProvider"
    val URL = "content://" + PROVIDER_NAME + "/ACTABLE"
    val CONTENT_URI = Uri.parse(URL)

    val _ID = "_ID"
    val NAME = "NAME"
    val MEANING = "MEANING"

    const val uriCode = 1

    private val sUriMatcher = UriMatcher(UriMatcher.NO_MATCH).apply {
        addURI(PROVIDER_NAME, "ACTABLE", uriCode)
    }

}

lateinit var db: SQLiteDatabase


override fun onCreate(): Boolean {
    var helper = DBHelper(getContext())
    db = helper.writableDatabase

    return if (db == null) false else true
}

override fun query(
    uri: Uri,
    cols: Array<out String>?,
    condition: String?,
    condition_val: Array<out String>?,
    order: String?
): Cursor? {
    return db.query("ACTABLE", cols, condition, condition_val, null, null, order)
}

override fun getType(p0: Uri): String? {
    return "vnd.android.cursor.dir/vnd.com.example.cptest.ACTABLE"
}

override fun insert(uri: Uri, cv: ContentValues?): Uri? {
    db.insert("ACTABLE", null, cv)
    context?.contentResolver?.notifyChange(uri, null)
    return uri
}

override fun delete(uri: Uri, condition: String?, condition_val: Array<out String>?): Int {
    var count = db.delete("ACTABLE", condition, condition_val)
    context?.contentResolver?.notifyChange(uri, null)
    return count
}

override fun update(
    uri: Uri,
    cv: ContentValues?,
    condition: String?,
    condition_val: Array<out String>?
): Int {
    var count = db.update("ACTABLE", cv, condition, condition_val)
    context?.contentResolver?.notifyChange(uri, null)
    return count
}

}

DBHelperClass :

class DBHelper(context: Context?) : SQLiteOpenHelper(context,"ACDB",null,1){
override fun onCreate(db: SQLiteDatabase?) {
    if (db != null) {
        db.execSQL("CREATE TABLE ACTABLE(_ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, MEANING TEXT)")
        db.execSQL("INSERT INTO ACTABLE (NAME, MEANING) VALUES ('ttt','BACHELOR OF COMPUTER SCIENCE')")
        db.execSQL("INSERT INTO ACTABLE (NAME, MEANING) VALUES ('MCA1','MASTERS OF COMPUTER SCIENCE')")
        db.execSQL("INSERT INTO ACTABLE (NAME, MEANING) VALUES ('BCA','BACHELOR OF COMPUTER SCIENCE')")
    }

    }

override fun onUpgrade(p0: SQLiteDatabase?, p1: Int, p2: Int) {

}

}

Androidmanifest.xml (Client App)

<uses-permission android:name = "com.example.cptest.PERMISSION"/>

Testing Code :

val PROVIDER_NAME = "com.example.cptest.ACProvider"
            val URL = "content://" + PROVIDER_NAME + "/ACTABLE"
            val CONTENT_URI = Uri.parse(URL)

            var rs = contentResolver.query(CONTENT_URI,
                arrayOf("_ID","NAME","MEANING"),null, null, null)

            if (rs != null) {
                if (rs.moveToNext())
                    Toast.makeText(applicationContext, rs.getString(1) + "\n"+ rs.getString(2), Toast.LENGTH_LONG).show()
            }
            else
            {
                Toast.makeText(applicationContext, "No DATA", Toast.LENGTH_LONG).show()
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文