1 year ago
#346036
Shahid Hussain
ContentResolver (Client App) Can't Get Data from Content Provider (Server App)
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()
}
android
kotlin
android-permissions
android-contentprovider
android-contentresolver
0 Answers
Your Answer