device012    device013  

由於撥打電話屬於手機底層的服務,與使用者隱私及通話費用息息相關,所以必須取得相關權限,首先要在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.widget.Button;
import android.widget.EditText;

public class PhoneNumberValid extends Activity {
    private EditText myEditText01;
    private Button myButton01;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    myEditText01 = (EditText)findViewById(R.id.EditText01);
    myButton01 = (Button)findViewById(R.id.Button01);
    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_DIAL, Uri.parse("tel:"+strInput));

        //改成ACTION_CALL則會直接撥出指定的電話號碼,不經由撥號程式
        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"
>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/EditText01"
android:phoneNumber="true"
/>
<Button
android:layout_marginTop="10sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Button01"
android:text="撥打電話"
/>
</LinearLayout>


arrow
arrow

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