Yang kamu butuhkan untuk membuat aplikasi ini adalah:
- Eclipse with ADT
- SDK Manager
Untuk mengaktifkan 2 permission diatas, buka file AndroidManifest.xml dan tambahkan uses-permission didalam tag manifest.
1
2
| <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission><uses-permission android:name="android.permission.CALL_PRIVILEGED"></uses-permission> |
1
| public class MainActivity extends ListActivity { |
1
2
3
4
5
6
7
8
| //Buat arrray 2 dimensi untuk data taksi yang terdiri dari nama dan nomor telepon private String[][] taxi_data = { { "Blue Bird", "0217941234" }, { "Express", "02126509000" }, { "Golden", "02165300333" }, { "Prima Jasa", "0217424525" }, { "Dian", "0215807070" }, { "Sri Medali", "0218005555" }, { "Putra", "0217815777" } }; //Buat arrray 1 dimensi untuk keperluan pembuatan menu list provider taxi private String[] taxi_name = { "Blue Bird", "Express", "Golden", "Prima Jasa", "Dian", "Sri Medali", "Putra", "Exit" }; |
1
2
3
4
5
6
7
8
| public void onCreate(Bundle icicle) { super.onCreate(icicle); // Create an ArrayAdapter, that will actually make the Strings above // appear in the ListView // Menset nilai array ke dalam list adapater sehingga data pada array // akan dimunculkan dalam list this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, taxi_name)); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| @Override /**method ini akan mengoveride method onListItemClick yang ada pada class List Activity * method ini akan dipanggil apabilai ada salah satu item dari list menu yang dipilih */ protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); // Get the item that was clicked // Menangkap nilai text yang dklik// Object o = this.getListAdapter().getItem(position);// String pilihan = o.toString(); String pilihan = this.getListAdapter().getItem(position).toString(); // Mencek pilihan, apabila pilihan = Exit maka akan keluar dari aplikasi if (pilihan.equals("Exit")) { finish(); } else {// pilihan != exit maka akan memanggil method callTaxi callTaxi(pilihan); } } |
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
| /** * Launches the activity to make phone call to taxi provider based on * selected taxi * */ protected void callTaxi(String pilihan) { try { // Intent digunakan untuk sebagai pengenal suatu activity untuk // membuat panggilan telepon Intent callIntent = new Intent(Intent.ACTION_CALL); String phonenumber = ""; for (int i = 0; i < taxi_data.length; i++) { if (pilihan.equals(taxi_data[i][0])) { phonenumber = taxi_data[i][1]; } } if (phonenumber.equals("")) { Toast.makeText(this, "Provider Taxi is not register", Toast.LENGTH_LONG).show(); return; } callIntent.setData(Uri.parse("tel:" + phonenumber)); startActivity(callIntent); } catch (Exception e) { e.printStackTrace(); } } |
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
| package org.nanda.example;import android.app.Activity;import android.app.ListActivity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.Toast;public class MainActivity extends ListActivity { //Buat arrray 2 dimensi untuk data taksi yang terdiri dari nama dan nomor telepon private String[][] taxi_data = { { "Blue Bird", "0217941234" }, { "Express", "02126509000" }, { "Golden", "02165300333" }, { "Prima Jasa", "0217424525" }, { "Dian", "0215807070" }, { "Sri Medali", "0218005555" }, { "Putra", "0217815777" } }; //Buat arrray 1 dimensi untuk keperluan pembuatan menu list provider taxi private String[] taxi_name = { "Blue Bird", "Express", "Golden", "Prima Jasa", "Dian", "Sri Medali", "Putra", "Exit" }; public void onCreate(Bundle icicle) { super.onCreate(icicle); // Create an ArrayAdapter, that will actually make the Strings above // appear in the ListView // Menset nilai array ke dalam list adapater sehingga data pada array // akan dimunculkan dalam list this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, taxi_name)); } @Override /**method ini akan mengoveride method onListItemClick yang ada pada class List Activity * method ini akan dipanggil apabilai ada salah satu item dari list menu yang dipilih */ protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); // Get the item that was clicked // Menangkap nilai text yang dklik// Object o = this.getListAdapter().getItem(position);// String pilihan = o.toString(); String pilihan = this.getListAdapter().getItem(position).toString(); // Mencek pilihan, apabila pilihan = Exit maka akan keluar dari aplikasi if (pilihan.equals("Exit")) { finish(); } else {// pilihan != exit maka akan memanggil method callTaxi callTaxi(pilihan); } } /** * Launches the activity to make phone call to taxi provider based on * selected taxi * */ protected void callTaxi(String pilihan) { try { // Intent digunakan untuk sebagai pengenal suatu activity untuk // membuat panggilan telepon Intent callIntent = new Intent(Intent.ACTION_CALL); String phonenumber = ""; for (int i = 0; i < taxi_data.length; i++) { if (pilihan.equals(taxi_data[i][0])) { phonenumber = taxi_data[i][1]; } } if (phonenumber.equals("")) { Toast.makeText(this, "Provider Taxi is not register", Toast.LENGTH_LONG).show(); return; } callIntent.setData(Uri.parse("tel:" + phonenumber)); startActivity(callIntent); } catch (Exception e) { e.printStackTrace(); } }} |
Misalnya klik Blue Bird, maka langsung masuk ke dialer.
Sekian tutorial dari saya, Let's Rock!
No comments:
Post a Comment