Android 實現手機撥打到電話的功能_Android

來源:互聯網
上載者:User

一部手機最常用的功能就是打電話和發簡訊了,在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 開發撥打到電話的簡單樣本,後續繼續補充相關資料,謝謝大家對本站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.