简体   繁体   中英

How to make phone call using intent after getting contact name and number

I am a newbie on android. I already made an activity that shows all my contact list and number inside Linear Layout. How can I make phone call using intent after I click the Linear Layout? I don't know where to put the setOnclickListener in my code. So here I share all of the source-code.

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/listContacts"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:clickable="true"
              android:orientation="vertical">

    <TextView
            android:id="@+id/itemName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:text="Nama : "
            style="@style/Base.TextAppearance.AppCompat.Headline"/>

    <TextView
            android:id="@+id/itemNumber"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:text="Nomor HP : "
            style="@style/Base.TextAppearance.AppCompat.Body2"/>
      </LinearLayout>

MainActivity.kt

class MainActivity : AppCompatActivity() {

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

        showDetail.setOnClickListener {
            var myDetailIntent = Intent(this, DetailActivity::class.java)
            startActivity(myDetailIntent)
        }

        doAsync {
            Thread.sleep(5000L)
            uiThread {
                showNotivy();
            }
        }
    }

    private fun showNotivy() {
        val notfyDetailIntent = Intent(this@MainActivity,
            DetailActivity::class.java)

        val myPendingIntent = TaskStackBuilder.create(this)
            .addParentStack(DetailActivity::class.java)
            .addNextIntent(notfyDetailIntent)
            .getPendingIntent(110, PendingIntent.FLAG_UPDATE_CURRENT)

        val myNotfyManager = this.getSystemService(android.content.Context.NOTIFICATION_SERVICE) as NotificationManager

        val myBuilder = NotificationCompat.Builder(this)
            .setContentTitle("Show Detail Contact")
            .setContentText("Click Me !")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(myPendingIntent)
            .setAutoCancel(true)
        myNotfyManager.notify(1101, myBuilder.build())
    }
}

DetailActivity.kt

class DetailActivity : AppCompatActivity(), LoaderManager.LoaderCallbacks<Cursor> {

    var DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME
    var NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER
    val myListContact : MutableList<myContact> = ArrayList();

    override fun onCreateLoader(p0: Int, p1: Bundle?): Loader<Cursor> {
        val MyContentUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI
        val myProjection = arrayOf(DISPLAY_NAME, NUMBER)
        return CursorLoader(this, MyContentUri, myProjection, null, null, DISPLAY_NAME+ " ASC")
    }

    override fun onLoadFinished(p0: Loader<Cursor>, p1: Cursor?) {
        myListContact.clear()
        if (p1!=null) {
            p1.moveToFirst()
            while(!p1.isAfterLast()) {
                myListContact.add(myContact(nama = p1.getString(p1.getColumnIndex(DISPLAY_NAME)),nomorHp = p1.getString(p1.getColumnIndex(NUMBER))))
                p1.moveToNext()
            }
        }

        val contactAdapter = myAdapterRecyView(myListContact)
        myRecyView.apply {
            layoutManager = LinearLayoutManager(this@DetailActivity)
            adapter = contactAdapter
        }
    }

    override fun onLoaderReset(p0: Loader<Cursor>) {
        val contactAdapter = myAdapterRecyView(myListContact)
        myRecyView.apply {
            layoutManager = LinearLayoutManager(this@DetailActivity)
            adapter = contactAdapter
        }
    }

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

        LoaderManager.getInstance(this).initLoader(1, null, this)
    }
}

myAdapterRecyView.kt

class myAdapterRecyView(private  val contact : List<myContact>): RecyclerView.Adapter<myHolder>() {
    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): myHolder {
        return myHolder(LayoutInflater.from(p0.context)
            .inflate(R.layout.layout_recy_view,p0,false))
    }

    override fun getItemCount(): Int  = contact.size
    override fun onBindViewHolder(p0: myHolder, p1: Int) {
        p0.bindContact(contact[p1])
    }
}


class myHolder(view: View):RecyclerView.ViewHolder(view) {
    private  val contactName = view.itemName
    private  val contactNumber = view.itemNumber

    fun bindContact(tmp: myContact) {
        contactName.text = "${contactName.text} ${tmp.nama}"
        contactNumber.text = "${contactNumber.text} ${tmp.nomorHp}"
    }
}


class myHolder(view: View):RecyclerView.ViewHolder(view) {
    private  val contactName = view.itemName
    private  val contactNumber = view.itemNumber

    fun bindContact(tmp: myContact) {
        contactName.text = "${contactName.text} ${tmp.nama}"
        contactNumber.text = "${contactNumber.text} ${tmp.nomorHp}"
    }
}

myContact.kt

class myContact  (
val nama : String,
val nomorHp : String
)

You can put yout click action inside the onBind in recycler view adapter.


class myHolder(view: View):RecyclerView.ViewHolder(view) {
    private  val contactName = view.itemName
    private  val contactNumber = view.itemNumber

    fun bindContact(tmp: myContact) {
        contactName.text = "${contactName.text} ${tmp.nama}"
        contactNumber.text = "${contactNumber.text} ${tmp.nomorHp}"

        view.setOnClickListener {
           // YOUR ACTION HERE.
        }
    }
}


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM