device014   device015  

Intent屬性: ACTION_CALL 直接撥號     ACTION_DIAL 啟動撥號程式

本範例為ACTION_CALL 搭配ImageButton

由於撥打電話屬於手機底層的服務,與使用者隱私及通話費用息息相關,所以必須取得相關權限,首先要在AndroidManifest中新增一個uses-permission,並宣告android:name=”android.permission.CALL_PHONE”使用權限。

java檔

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

public class PhoneNumberValid extends Activity {
private EditText myEditText01;
private ImageButton myButton01;
/** Called when the activity is first created. */
@Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.main);
    myEditText01 = (EditText)findViewById(R.id.EditText01);
    myButton01 = (ImageButton)findViewById(R.id.ImageButton01);
    myButton01.setOnClickListener(new Button.OnClickListener(){
        @Override
        public void onClick(View v) {
        // TODO Auto-generated method stub
        String strInput = myEditText01.getText().toString();
        Intent myIntentDial = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+strInput));
        startActivity(myIntentDial);
        }
    });
  }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_margin="10sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請輸入電話號碼:"
android:textSize="20sp"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<EditText
android:layout_width="250sp"
android:layout_height="wrap_content"
android:id="@+id/EditText01"
android:phoneNumber="true"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ImageButton01"
android:src="@drawable/p_call"
android:layout_marginLeft="5sp"
/>
</LinearLayout>
</LinearLayout>

arrow
arrow

    S 發表在 痞客邦 留言(1) 人氣()