一部手機最常用的功能就是打電話和發簡訊了,在Android開發中我們如何通過程式撥打到電話呢?本文就給出一個用Android手機撥打到電話的簡單的執行個體。
下面是開發此執行個體的具體步驟:
一、建立一個Android工程,命名為phoneCallDemo。
二、設計程式的介面,開啟main.xml把內容修改如下:
XML/HTML代碼
<?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_width="fill_parent" android:layout_height="wrap_content" android:text="Please input the phoneNumer:" /> <EditText android:id="@+id/et1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:phoneNumber="true" /> <Button android:id="@+id/bt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Call Phone" /> </LinearLayout>
三、增加撥打到電話的許可權,開啟AndroidManifest.xml,修改代碼如下:
XML/HTML代碼
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.test" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".PhoneCallDemo" 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-sdk android:minSdkVersion="3" /> <uses-permission android:name="android.permission.CALL_PHONE"> </uses-permission> </manifest>
四、主程式phoneCallDemo.java代碼如下:
package com.android.test;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; import android.widget.Toast; public class PhoneCallDemo extends Activity { private Button bt; private EditText et; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //取得資源 bt = (Button)findViewById(R.id.bt1); et = (EditText)findViewById(R.id.et1); //增加事件響應 bt.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { //取得輸入的電話號碼串 String inputStr = et.getText().toString(); //如果輸入不為空白建立打電話的Intent if(inputStr.trim().length()!=0) { Intent phoneIntent = new Intent("android.intent.action.CALL", Uri.parse("tel:" + inputStr)); //啟動 startActivity(phoneIntent); } //否則Toast提示一下 else{ Toast.makeText(PhoneCallDemo.this, "不能輸入為空白", Toast.LENGTH_LONG).show(); } } }); }
以上就是Android 開發撥打到電話的簡單樣本,後續繼續補充相關資料,謝謝大家對本站的支援!