Cara Buat Aplikasi Android Call Dial
Ini Adalah Source Code Antara Untuk MainActivity.java
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 EditText number;
private Button call;
private Button dial;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number = (EditText) findViewById(R.id.editText1);
call = (Button) findViewById(R.id.button1);
dial = (Button) findViewById(R.id.button2);
MyPhoneListener PhoneListener = new MyPhoneListener();
TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
call.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();
}
}
});
dial.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;
}
}
Ini Adalah Source Code Untuk File 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/back"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText1"
android:textColor="#ffffffff"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:hint="Type a number to call"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="Call" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="16dp"
android:text="Dial Call" />
</RelativeLayout>
Dan Inilah Hasil Running Akhir Dari Source Code Di Atas
Komentar
Posting Komentar