“Call And Dial"
Assalamulaikum, Wr. Wb.
Pada laporan kali ini saya akan membuat aplikasi sederhana Call & Dial. Disini saya membuat Aplikasi android dengan menggunankan ADT.
- Membuat projek baru di editor yang akan kita gunakan seperti ADT, Android Studio, Dll.
File > New > Android Application Project dan beri nama pada aplikasi yang akan kita buat, contoh “Call and Dial”
- Kemudian kita akan mengantur file xmlnya dengan potongan script seperti dibawah ini:
Activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/logo"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/phoneNumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Type a number to call"
android:ems="10"
android:layout_marginTop="50dp"
android:inputType="phone"
/>
<Button
android:id="@+id/call"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/phoneNumber"
android:layout_marginTop="20dp"
android:text="Call"
/>
<Button
android:id="@+id/dial"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/call"
android:layout_marginTop="20dp"
android:text="Dial And Call"/>
</RelativeLayout>
Hasil tampilan sebagai berikut :
- kemudiian kita ubah coding file java di folder src seperti script dibawah ini :
package com.riski.calldial;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button callBtn;
private Button dialBtn;
private EditText number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number = (EditText)findViewById(R.id.phoneNumber);
callBtn = (Button)findViewById(R.id.call);
dialBtn = (Button)findViewById(R.id.dial);
MyPhoneListener phonelistener = new MyPhoneListener();
TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phonelistener, PhoneStateListener.LISTEN_CALL_STATE);
callBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
String uri = "tel:" + number.getText().toString();
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(uri));
startActivity(callIntent);
}catch (Exception e){
Toast.makeText(getApplicationContext(), "Your Call has failed", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
});
dialBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
String uri = "tel:" + number.getText().toString();
Intent dialIntent = new Intent(Intent.ACTION_DIAL, Uri.parse(uri));
startActivity(dialIntent);
}catch (Exception e){
Toast.makeText(getApplicationContext(), "Your Call has failed", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
});
}
private class MyPhoneListener extends PhoneStateListener{
private boolean onCall = false;
@Override
public void onCallStateChanged(int state, String incomingNumber){
switch (state){
case TelephonyManager.CALL_STATE_RINGING:
Toast.makeText(MainActivity.this, incomingNumber + "calls you", Toast.LENGTH_SHORT).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(MainActivity.this, "on call", Toast.LENGTH_SHORT ).show();
onCall = true;
break;
case TelephonyManager.CALL_STATE_IDLE:
if (onCall == true){
Toast.makeText(MainActivity.this, "restart app after", Toast.LENGTH_SHORT).show();
Intent restart = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
restart.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(restart);
onCall = false;
}
break;
default:
break;
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Hasil Running :
Tidak ada komentar:
Posting Komentar