Android Application

Kotlin 콘텐트 프로바이더(Content Provider) : 통화 기록을 가져오기

작성자 임베디드코리아 작성일23-10-23 23:14 조회1,427회 댓글0건

첨부파일

Kotlin 콘텐트 프로바이더(Content Provider) : 통화 기록을 가져오기

-------------------------------------------------------------------------------------
      activity_main.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btnCall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="통화 기록 가져오기" />

    <EditText
        android:layout_weight="1"
        android:id="@+id/edtCall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:background="#eee8aa" />
</LinearLayout>

------------------------------------------------------------------------------------
    MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
    lateinit var btnCall: Button
    lateinit var edtCall: EditText

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.READ_CALL_LOG), Context.MODE_PRIVATE)

        btnCall = findViewById<Button>(R.id.btnCall)
        edtCall = findViewById<EditText>(R.id.edtCall)

        btnCall.setOnClickListener {
            edtCall.setText(findCallHistory())
        }
    }

    fun findCallHistory(): String {
        var callSet = arrayOf(CallLog.Calls.DATE, CallLog.Calls.TYPE, CallLog.Calls.NUMBER, CallLog.Calls.DURATION)

        var c = contentResolver.query(CallLog.Calls.CONTENT_URI, callSet, null, null, null)

        if ( c!!.count == 0 )
            return "통화기록 없음"

        var callBuff = StringBuffer() //  ִ  100  ȭ
        callBuff.append("\n      날짜        :  구분  :      전화번호      :  통화시간 \n\n")
        c.moveToFirst()

        do {
            var callDate = c.getLong(0)
            var datePattern = SimpleDateFormat("yyyy-MM-dd")
            var date_str = datePattern.format(Date(callDate))
            callBuff.append(" $date_str  :")
            if (c.getInt(1) == CallLog.Calls.INCOMING_TYPE)
                callBuff.append("  착신  :  ")
            else
                callBuff.append("  발신  :  ")
            callBuff.append(c.getString(2) + "  :        ")
            callBuff.append(c.getString(3) + "초\n")
        } while (c.moveToNext())

        c.close()
        return  callBuff .toString
    }
}

------------------------------------------------------------------------------------
    themes.xml
------------------------------------------------------------------------------------
<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Base.Theme.ContentProviderCallDATA" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">#FF9800</item>
        <item name="colorPrimaryDark">#00897B</item>
    </style>

    <style name="Theme.ContentProviderCallDATA" parent="Base.Theme.ContentProviderCallDATA" />
</resources>