API In Kotlin

Introduction

 
API is an application program interface used to develop applications using tools and utilities. API specifies the user interface with the application and to create a user friendly application with the tools and sensor. API has Database, Loader, Frameworks, Contacts.
 
Database
 
There are two types in using the databases. One is using the SQLite library and the other is using the room architecture component for storing the data. The latter adds an abstraction layer between the database storage objects. Room is a support architecture component and it should be configured in the platform of Android studio. For configuration, open a module bild.gradle file applies plugin: 'kotlin-kapt'. It is a kotlin compiler plugin that supports annotation processing. Room consists of three objects, Database, Entity, Data Access Object.
 
Entity
 
Represent holder for the database. A database contains several entity containers.
 
Data Access Object
 
It contains access logic to retrieve data from the database. It serves as an interface between program logic and database model.
 
Declaration of database
  1. import android.arch.persistence.room.*      
  2. @Database (entities = arrayOf (Employee :: class, Contact ::class), version = 1)      
  3. abstract class MyDatabase : RoomDatabase()      
  4. {      
  5.  abstract fun employeeDao() : EmployeeDao      
  6.  abstract fun contactDao() : ContactDao      
  7.  abstract fun personDao() : PersonDao      
  8. }     
The entity classes are declared inside the @Database annotation. The verion number is used to upgrade diffrent data model versions.
 
Entity
  1. @Entity  
  2. data class Employee (@PrimaryKey(autoGenerate = true) var ud:Int = 0.  
  3. var firstName:String,  
  4. var lastName:String)  
  5.   
  6. @Entity  
  7. data class Contact (@Primarykey(autoGenerate = true) var uid:Int = 0,  
  8. var emailAddr : String)  
The primary key of type Int for each entity is required. The column names from a database defined by these entity class match the variable name. The table name is taken from the entity class name.
 
Nested Object
 
It is impossible to define inter-object relations other than manually by foreign keys, only by defining a nesting of hierarchical objects.
  1. @Entity  
  2. data class Employee (  
  3. AprimaryKey (autoGenerate = true) var uid:Int = 0,  
  4. var firstName ::String,  
  5. var lastName ::String)  
Loader
 
The loader is used to load the data in the background. Loader is used to react on callback events and the need to load the data in a presumably time-consuming process arises either in UI thread. The main responsibilities of the class consist of constructing an andrid.content.Loader and providing loading state callback functions.
 
The loader_id is a unique id for a loader. An app has several loaders, so the loader framework needs to be able to distinguish between the number of loaders and null will receive the result from the loading process.
 
It can implement and provide any type of subclass of LoaderManager.
  1. class Mainactivity : AppCompatActivity (),  
  2.  LaoderManager.LoaderCallbacks<MyData>  
  3. {  
  4.   val LOADER_ID =42  
  5.   var loaded:MyData? = null  
  6. // other fields and methods  
  7.   
  8. override fun onCreateLoader (id: Int, args: Bundle?):  
  9.   Loader <MyData>?  
  10. {  
  11.  Log.e ("LOG""onCreateLoader()")  
  12.  return makeLoader()  
  13. }  
  14.   
  15. override fun onLoadFinished (loader : Loader <MyData>?,  
  16.   data: MyData?)  
  17. {  
  18.   Log.e ("LOG""Load finished: " +data)  
  19.   loaded = data  
  20.   // show on UI   
  21. }  
  22. override fun onLoaderReset (Loader: Loader <MyData>?)  
  23. {  
  24.  Log.e("LOG""onLoaderReset()")  
  25.  loaded = null  
  26.   //remove froom ui   
  27. }  
  28. }  
Expandable notifiation
 
Notifications don't have to contain short message only, using the expandable features, it is possible to show larger amount of information to the user. Android created expandable notifications in the search engine to find corresponding pages.
 
Contacts
 
Managing and using contacts is one of the task handheld device. The complexity can be reduced if restrict at the back-end part and omit user interface. Finding out how to read contacts data and write contacts data and synchronizing contacts. 
 
Synchronizing contact build a subclass of android.app.Application and register name inside <application> tags of file Androifmanifest.xml. build a bindable service component the system can use for synchronization.
 
Implement the SyncAdapter.
 
QuickBadges
 
Quick badge allows to GUI widgit the user to see a contact detail and take any suitable action from there. To generate a quick contact badge, add the code inside the layout.
  1. <QuickContactBadge  
  2.              android:id = "@+id/quickBadge"  
  3.              android:layout_width  "60dp"  
  4.              android:lyout_height = "60dp"  
  5.              androd:scaleType = "centerCrop"  
  6. /> 


Similar Articles