먼저 안드로이드 패키지를 생성한다 (main) 생성후에 src main.java에서 소스를 아래와 같이 수정을 한다.
이번 예제는 전화기 자판으로 전화를 거는 것이 아니라 안드로이드 프로그램 내에서 전화거는 방법에 대한 소스이다. 기술적으로 아주 간단하며 프로그램내에 전화걸수 있는 기능을 삽입할수 있다.
화면은 전화번호를 입력할수 있는 에디터 박스와 전화걸리 버튼으로 아주 심플하게 이루어져 있다. 최대한 기능에 대한 소스만 집어넣어 파악하기 쉽도록 하기 위함이다. 전체소스는 다음과 같다
package my.main;
import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout;
public class main extends Activity { EditText mEdtNumber = null; LinearLayout mLinearLayout = null; Button mButton_dial = null;
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
mLinearLayout = new LinearLayout(this);
mEdtNumber = new EditText(this); mEdtNumber.setText("010-0000-0000"); mLinearLayout.addView(mEdtNumber);
mButton_dial = new Button(this); mButton_dial.setText("전화걸기"); mLinearLayout.addView(mButton_dial); // 버튼 이벤트 mButton_dial.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { performDial(); } });
setContentView(mLinearLayout); }
public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_CALL) { performDial(); return true; } return false; } /** 전화걸기 실행 함수 */ public void performDial() { if (mEdtNumber != null) { try { startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+ mEdtNumber.getText()))); } catch (Exception e) { e.printStackTrace(); } } } }
/** 바로전화걸기 실행 함수 */
//startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + mEdtNumber.getText())));

옵션인 ACTION_DIAL 은 휴대폰 다이얼패스에서 전화거는 것과 같은 프로그램을 띄워준다.

|