2013年5月9日 星期四

[Android] 撥打電話(make a phone call)

-需繼承PhoneStateListener監聽通話狀態(接通/閒置/來電)
-需在AndroidManifest.xml加入android.permission.CALL_PHONE和android.permission.READ_PHONE_STATE二個權限。

1.MainActivity.java
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

 final Context context = this;
 private Button button;
 private EditText editText;
 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  button = (Button) findViewById(R.id.buttonCall);
  editText=(EditText)findViewById(R.id.editText1);  
  Log.e("log",  this.toString());
  // 初始電話服務功能
  PhoneCallListener phoneListener = new PhoneCallListener();
  TelephonyManager telephonyManager = (TelephonyManager) this
    .getSystemService(Context.TELEPHONY_SERVICE);
  telephonyManager.listen(phoneListener,
    PhoneStateListener.LISTEN_CALL_STATE);

  // 監聽按鈕
  button.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View arg0) {

    Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel:"+editText.getText().toString()));
    startActivity(callIntent);

   }

  });

 }

 private class PhoneCallListener extends PhoneStateListener {

  private boolean isPhoneCalling = false;

  @Override
  public void onCallStateChanged(int state, String incomingNumber) {

   if (TelephonyManager.CALL_STATE_RINGING == state) {
    // 來電時
    Log.i("Log", "RINGING, number: " + incomingNumber);
   }

   if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
    // 通話中
    isPhoneCalling = true;
   }

   if (TelephonyManager.CALL_STATE_IDLE == state) {
    // 結束通話時
    if (isPhoneCalling) {

     // 重新啟動APP,這裡的目的是要回到activity畫面
     // 如果沒有這一段,通話結束則會停在通話明細的畫面(call list)
     Intent i = getBaseContext().getPackageManager()
       .getLaunchIntentForPackage(
         getBaseContext().getPackageName());
     
     i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_SINGLE_TOP );
     startActivity(i);

     isPhoneCalling = false;
    }

   }
  }
 }
}


2.在EditText裡需加入inputType="phone"屬性,這是為了啟動電話的鍵盤的風格。

main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/buttonCall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="42dp"
        android:layout_toRightOf="@+id/editText1"
        android:text="CALL" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/buttonCall"
        android:layout_alignParentLeft="true"
        android:ems="10"
        android:inputType="phone" />

</RelativeLayout>
3.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.phonecall"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

 <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
         >
  
        <activity
            android:label="@string/app_name"
            android:name=".MainActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
結果:

沒有留言:

張貼留言