主要的代碼實現
PhoneCaller.java
package cn.com.android.phone;</p><p>import android.app.Activity;<br />import android.content.Intent;<br />import android.net.Uri;<br />import android.os.Bundle;<br />import android.view.View;<br />import android.view.View.OnClickListener;<br />import android.widget.Button;<br />import android.widget.EditText;</p><p>public class PhoneCaller extends Activity {<br /> private EditText editText;<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);</p><p> editText = (EditText) findViewById(R.id.editText);</p><p> Button button = (Button) findViewById(R.id.myButton);<br /> button.setOnClickListener(new OnClickListener(){</p><p>public void onClick(View v) {<br />String number = editText.getText().toString();<br />// 聲明打電話的意圖<br />Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+number));<br />// 將意圖傳遞給作業系統<br />PhoneCaller.this.startActivity(intent);<br />}</p><p> });</p><p> }<br />}
main.xml代碼
<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> ><br /><TextView<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:text="@string/hello"<br /> /><br /> <EditText<br /> android:id="@+id/editText"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> /></p><p> <Button<br /> android:id="@+id/myButton"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:text="@string/button"<br /> /><br /></LinearLayout><br />
這裡一定要注意,因為我們撥打到電話是使用的android系統本身攜帶的功能,所以一定要進行許可權說明,許可權說明定義在AndroidManifest.xml檔案當中。現在我們看看這個AndroidManifest檔案的代碼:
<?xml version="1.0" encoding="utf-8"?><br /><manifest xmlns:android="http://schemas.android.com/apk/res/android"<br /> package="cn.com.android.phone"<br /> android:versionCode="1"<br /> android:versionName="1.0"><br /> <uses-sdk android:minSdkVersion="8" /></p><p> <application android:icon="@drawable/icon" android:label="@string/app_name"><br /> <activity android:name=".PhoneCaller"<br /> android:label="@string/app_name"><br /> <intent-filter><br /> <action android:name="android.intent.action.MAIN" /><br /> <category android:name="android.intent.category.LAUNCHER" /><br /> </intent-filter><br /> </activity></p><p> </application><br /> <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission><br /></manifest>
需要注意的是:
第一,語句<uses-permission android:name="android.permission.CALL_PHONE"/>的位置,它與application是同級的。
第二,如何尋找使用者權限呢,可以通過api下,android.permission這個類來進行尋找
我們知道,一個電話是沒法打電話的,我們想類比打電話,那麼肯定要最少有兩個電話,這時候,我們就要啟動另一個avd了。
這裡需要注意的是,其連接埠號碼,就是電話號碼。
,左上方的5554.
----------------------------------------------------------------
有些時候,我們並不像使用者直接把電話打出去,而是只是單純的想調出來撥打到電話的那個節目,這時候我們應該怎麼辦呢,這裡就讓我們一起來一下。
private void caller1(){
Intent intent = new Intent(Intent.ACTION_CALL_BUTTON);
startActivity(intent);
}
該intent也可以通過Intent intent = new Intent("android.intent.action.CALL_BUTTON");來進行聲明,本質上實際上是一樣的。
有關和電話撥號有關的許可權,基本上如下:
Android.permission.CALL_PHONE:允許程式傳入電話撥號直接進行撥號,不用通過電話介面進行確認。
Android.permission.CALL_PRIVILEGED:允許程式將電話傳給撥號程式,使用者進行介面確認之後才會進行撥號。