2015年3月13日 星期五

Android Call Phone

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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <LinearLayout
        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:inputType="textPersonName"
            android:text=""
            android:ems="10"
            android:id="@+id/edt_PhoneNumber"
             />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/btn_CallPhone" />
    </LinearLayout>

</RelativeLayout>

MainActivity.java
package com.example.win7.make_a_call;

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 MainActivity extends Activity {
    private EditText edt_PhoneNumber;
    private Button btn_CallPhone;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edt_PhoneNumber=(EditText)findViewById(R.id.edt_PhoneNumber);
        btn_CallPhone=(Button)findViewById(R.id.btn_CallPhone);
        btn_CallPhone.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View view) {
                try {
                    /*getText()取得使用者輸入號碼*/
                    String phonenumber = edt_PhoneNumber.getText().toString();
                    //建立一個Intent並且在建構式中加入action.CALL字串用來設定所建立的Intent物件是用來打電話
                    //第二個參數使用Uri.parse()方法,並將使用者輸入號碼傳入
                    Intent intent_CallPhone = new Intent("android.intent.action.CALL", Uri.parse("tel:"+phonenumber));
                    startActivity(intent_CallPhone);
                    edt_PhoneNumber.setText("");
                }catch (Exception ex){
                    ex.printStackTrace();
                }

            }
            //start
        });
    }
}



AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.win7.make_a_call" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <!--打電話權限-->
    <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
</manifest>

沒有留言:

張貼留言