Membuat Panggilan Telepon dari Aplikasi
Telepon merupakan fungsi/ fitur dasar dari smartphone yang kini telah berkembang pesat. Meskipun kini telpon bukan menjadi minat utama dari para pengguna smartphone namun telepon masih menjadi salah satu solusi andalan untuk melakukan voice call secara langsung. Berkembangnya Voice over Internet Protocol (VoIP) tidak terlalu menggoyahkan fungsi telepon itu sendiri. Berikut saya akan memberikan contoh simpel untuk melakukan dial dari aplikasi yang kita kembangkan.
Kesempatan ini saya akan memberikan 2 model contoh melakukan panggilan melalui aplikasi.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
package com.anonimeact.phonedial; import android.Manifest; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Toast; public class MainActivity extends AppCompatActivity { int code_permission_phone_direct = 99; int code_permission_phone_indirect = 88; // Seting nomor telepon String _no_telp = "0211111111xxx"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { if (requestCode == code_permission_phone_direct) if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { Intent intentDirect = new Intent(Intent.ACTION_CALL); intentDirect.setData(Uri.parse("tel:" + _no_telp)); startActivity(intentDirect); } else { Toast.makeText(this, "Anda tidak mengijinkan menggunakan telepon", Toast.LENGTH_SHORT).show(); } else if (requestCode == code_permission_phone_indirect) if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { Intent intentIndirect = new Intent(Intent.ACTION_CALL); intentIndirect.setData(Uri.parse("tel:" + _no_telp)); startActivity(intentIndirect); } else { Toast.makeText(this, "Anda tidak mengijinkan menggunakan telepon", Toast.LENGTH_SHORT).show(); } } public void DirectDial(View v) { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_CONTACTS)) { } else { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, code_permission_phone_direct); } } else { Intent intentDirect = new Intent(Intent.ACTION_CALL); intentDirect.setData(Uri.parse("tel:" + _no_telp)); startActivity(intentDirect); } } public void InDirectDial(View v) { Intent intent = new Intent(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:" + _no_telp)); if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_CONTACTS)) { } else { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, code_permission_phone_indirect); } } else { Intent intentIndirect = new Intent(Intent.ACTION_DIAL); intentIndirect.setData(Uri.parse("tel:" + _no_telp)); startActivity(intentIndirect); } } } |
Kode diatas mengimplementasikan 2 metode dial yang sedikit berbeda. Pertama pada bagian yang saya sebut DIRECT DIAL sedangkan bagian kedua saya sebut INDIRECT DIAL. Sebutan tersebut adalah saya buat untuk pribadi sendiri. Direct dial karena ketika kita memulai action menekan tombol telepon, aksi yang akan dilakukan adalah langsung menelpon nomor tujuan, sedangkan untukyang indirect dial menampilkan nomor telepon dahulu sebelum memanggil.
Direct dial
1 2 3 |
Intent intentDirect = new Intent(Intent.ACTION_CALL); intentDirect.setData(Uri.parse("tel:" + _no_telp)); startActivity(intentDire |
Direct dial menggunakan ACTION_CALL pada saat intent
Indirect dial
1 2 3 |
Intent intentIndirect = new Intent(Intent.ACTION_DIAL); intentIndirect.setData(Uri.parse("tel:" + _no_telp)); startActivity(intentIndirect); |
Indirect dial menggunakan ACTION_DIAL pada saat intent
1 2 3 |
Intent intentIndirect = new Intent(Intent.ACTION_DIAL); intentIndirect.setData(Uri.parse("tel:" + _no_telp)); startActivity(intentIndirect); |
Berikut adalah file activity_main.xml untuk membuat sederhananya tampilanya.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.anonimeact.phonedial.MainActivity" android:orientation="vertical" android:gravity="center"> <Button android:id="@+id/btn_call_direct" android:layout_width="200dp" android:layout_height="wrap_content" android:text="Direct Call" android:drawableLeft="@drawable/ic_phone_call" android:onClick="DirectDial" android:layout_marginBottom="20dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="or" android:layout_marginBottom="20dp"/> <Button android:id="@+id/btn_call_indirect" android:layout_width="200dp" android:layout_height="wrap_content" android:text="Indirect Call" android:onClick="InDirectDial" android:drawableLeft="@drawable/ic_phone_call"/> </LinearLayout> |