Pro Android with Kotlin
CHAPTER 6: Content Providers
Download 5.42 Mb. Pdf ko'rish
|
@de android telegram Pro Android with Kotlin Developing Modern Mobile
83
CHAPTER 6: Content Providers CalendarContract This is a rather complex content provider with many tables. As an example, we are accessing the calendars list and adding an event here: val havePermissions = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) == PackageManager.PERMISSION_GRANTED if(!havePermissions) { // Acquire permissions... }else{ data class CalEntry(val name: String, val id: String) val calendars = HashMap val uri = CalendarContract.Calendars.CONTENT_URI val cursor = contentResolver.query( uri, null, null, null, null) cursor.moveToFirst() while (!cursor.isAfterLast) { val calName = cursor.getString( cursor.getColumnIndex( CalendarContract.Calendars.NAME)) val calId = cursor.getString( cursor.getColumnIndex( CalendarContract.Calendars._ID)) calendars[calName] = CalEntry(calName, calId) cursor.moveToNext() } Log.e("LOG", calendars.toString()) val calId = "4" // You should instead fetch an // appropriate entry from the map! val year = 2018 val month = Calendar.AUGUST val dayInt = 27 val hour = 8 val minute = 30 val beginTime = Calendar.getInstance() beginTime.set(year, month, dayInt, hour, minute) val event = ContentValues() event.put(CalendarContract.Events.CALENDAR_ID, calId) event.put(CalendarContract.Events.TITLE, "MyEvent") event.put(CalendarContract.Events.DESCRIPTION, "This is test event") event.put(CalendarContract.Events.EVENT_LOCATION, "School") 84 CHAPTER 6: Content Providers event.put(CalendarContract.Events.DTSTART, beginTime.getTimeInMillis()) event.put(CalendarContract.Events.DTEND, beginTime.getTimeInMillis()) event.put(CalendarContract.Events.ALL_DAY,0) event.put(CalendarContract.Events.RRULE, "FREQ=YEARLY") event.put(CalendarContract.Events.EVENT_TIMEZONE, "Germany") val retUri = contentResolver.insert( CalendarContract.Events.CONTENT_URI, event) Log.e("LOG", retUri.toString()) } We didn’t implement the permission inquiry; permissions are described in detail in Chapter 7 . CallLog This is a table listing placed and received calls. Here’s an example to list the table: val havePermissions = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALL_LOG) == PackageManager.PERMISSION_GRANTED if(!havePermissions) { // Acquire permissions... }else { val uri = CallLog.Calls.CONTENT_URI val cursor = contentResolver.query( uri, 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() } } We didn’t implement the permission inquiry; permissions are described in detail in Chapter 7 . Table 6-3 describes the table columns. |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling