// 우선 메인 화면 디자인
res/layout/wiget3.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="call 114" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="call 112" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom"
/>
</LinearLayout>
// 버튼을 클릭했을 때
private static final String TAG = "Wiget3";
Button button1, button2, button3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wiget3);
button1 = (Button)findViewById(R.id.button1) ;
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
call("tel:114") ;
}
});
button2 = (Button)findViewById(R.id.button2) ;
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
call("tel:112") ;
}
});
button3 = (Button)findViewById(R.id.button3) ;
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
doCustom() ;
}
});
}
public void call(String phone) {
try {
Log.d(TAG, phone+" 호출입니다.") ;
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse(phone));
startActivity(callIntent);
} catch (ActivityNotFoundException e) {
Log.e("myphone dialer", "Call failed", e);
}
}
private void doCustom() {
Log.d(TAG, "사용자가 호출입니다.") ;
Toast.makeText(getBaseContext(), "사용자 호출입니다.", Toast.LENGTH_LONG).show() ;
}
//AndroidManifest.xml 추가(전화걸기)
<uses-permission android:name="android.permission.CALL_PHONE