Pro Android with Kotlin


CHAPTER 6: Content Providers


Download 5.42 Mb.
Pdf ko'rish
bet83/223
Sana04.11.2023
Hajmi5.42 Mb.
#1746807
1   ...   79   80   81   82   83   84   85   86   ...   223
Bog'liq
@de android telegram Pro Android with Kotlin Developing Modern Mobile

90
CHAPTER 6: Content Providers
showTable(MediaStore.Audio.Media.getContentUri(
"internal")) // <- other option: "external"
fun showTable(tbl:Uri) {
Log.e("LOG", "#######################################")
Log.e("LOG", tbl.toString())
val cursor = contentResolver.query(
tbl, null, null, null, null)
cursor.moveToFirst()
while (!cursor.isAfterLast) {
Log.e("LOG", "New entry:")
for(name in cursor.columnNames) {
val v = cursor.getString(
cursor.getColumnIndex(name))
Log.e("LOG"," > " + name + " = " + v)
}
cursor.moveToNext()
}
}
Settings
This is a content provider that deals with various global and system-level settings. The 
following are the main URIs as constants from the contract class:

Settings.Global.CONTENT_URI:
Global settings. All entries are triples of the following:

_id

android.provider.Settings.NameValueTable.NAME

android.provider.Settings.NameValueTable.VALUE

Settings.System.CONTENT_URI:
Global system-level settings. All entries are triples of the following:

_id

android.provider.Settings.NameValueTable.NAME

android.provider.Settings.NameValueTable.VALUE

Settings.Secure.CONTENT_URI:
This is a secured system setting. Apps are not allowed to alter it. All entries are triples of the 
following:

_id

android.provider.Settings.NameValueTable.NAME

android.provider.Settings.NameValueTable.VALUE


91
CHAPTER 6: Content Providers
To investigate these tables, take a look at the online API documentation of android.
provider.Settings. It describes all possible settings. To list the complete settings, you can 
use the same function as earlier for the ContactsContract contract class.
showTable(Settings.Global.CONTENT_URI)
showTable(Settings.System.CONTENT_URI)
showTable(Settings.Secure.CONTENT_URI)
...
fun showTable(tbl:Uri) {
Log.e("LOG", "##################################")
Log.e("LOG", tbl.toString())
val cursor = contentResolver.query(
tbl, null, null, null, null)
cursor.moveToFirst()
while (!cursor.isAfterLast) {
Log.e("LOG", "New entry:")
for(name in cursor.columnNames) {
val v = cursor.getString(
cursor.getColumnIndex(name))
Log.e("LOG"," > " + name + " = " + v)
}
cursor.moveToNext()
}
}
Your app don’t need special permission to read the settings. However, writing is possible 
only for the Global and System tables, and you also need a special construct to acquire 
permission.
if(!Settings.System.canWrite(this)) {
val intent = Intent(
Settings.ACTION_MANAGE_WRITE_SETTINGS)
intent.data = Uri.parse(
"package:" + getPackageName())
startActivity(intent)
}
Usually you acquire permissions by calling the following:
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.WRITE\_SETTINGS), 42)
However, when setting permissions, this request gets denied immediately by current Android 
versions. So, you cannot use it and need to call the intent as shown earlier instead.
To access a certain entry, you can again use constants and methods from the contract class.
val uri = Settings.System.getUriFor(
Settings.System.HAPTIC_FEEDBACK_ENABLED)
Log.e("LOG", uri.toString())
val feedbackEnabled = Settings.System.getInt(
contentResolver,
Settings.System.HAPTIC_FEEDBACK_ENABLED)
Log.e("LOG", Integer.toString(feedbackEnabled))



Download 5.42 Mb.

Do'stlaringiz bilan baham:
1   ...   79   80   81   82   83   84   85   86   ...   223




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling