Android學習筆記(4)—類比手機撥號器

來源:互聯網
上載者:User

      

      一直以來對手機之間的通話都很好奇,撥打號碼就能實現兩個手機連同,今天就可以實現這個小功能,不然還怎麼說自己是研究Android的手機的呢?


      Android的手機模擬器內建有撥號功能,我們先試試內建的撥號功能。我們啟動兩個Android 2.3.3版本的模擬器。你有沒有注意每個模擬器左上方有一個這樣的,只不過數字不同,這究竟是什麼含義呢?每個模擬器將會被綁定到“192.168.1.1”這個本地IP上,而後面的“5556”則是他的連接埠號碼,所以這個模擬器的唯一標識地是:“192.168.1.1:5556”,所以,這個連接埠號碼可以當作是我們的手機號。只要明白了這個,就不會困惑“沒有手機號怎麼撥打呢?”


      開啟手機號是“5554”的模擬器,輸入“手機號”5556,點擊“撥打鍵”,兩個手機則實現通話了:


 


     下面我們自己開發自己的手機撥號器。


     ●建立Android開發工程


    建立項目HTCMobile,選擇Android 的版本是2.3.3,項目結構如下所示:




     ●編寫strings.xml檔案


<?xml version="1.0"encoding="utf-8"?><resources>     <string name="hello">Hello World,HTCMobileActivity!</string>    <string name="app_name">HTC撥號器</string>    <string name="mobile_name" >請輸入手機號</string>    <string name="call">撥打此號</string></resources>這個檔案主要用來定義字串和數值●編寫main.xml檔案在項目的res/layout目錄下找到此檔案<?xml version="1.0"encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >   <TextView       android:layout_width="fill_parent"       android:layout_height="wrap_content"       android:text="@string/mobile_name" />     <EditText         android:id="@+id/phoneNo"         android:layout_width="fill_parent"       android:layout_height="wrap_content"        />     <Button        android:id="@+id/cllPhone"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/call"/> </LinearLayout>

      此檔案主要設定布局檔案,類似於我們的html分頁檔,在eclipse中我們可以點擊檔案編輯區的“Graphical Layout”預覽效果:




     ●編寫HTCMobileMobileActivity.java檔案


packagecom.sinosoft; importandroid.app.Activity;importandroid.content.Intent;importandroid.net.Uri;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;import android.widget.EditText; publicclass HTCMobileActivity extends Activity {    /** Called when the activity is firstcreated. */    @Override    public void onCreate(BundlesavedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        Button button=(Button)findViewById(R.id.cllPhone);        button.setOnClickListener(newView.OnClickListener() {                                                       public void onClick(Viewv) {                                     // TODOAuto-generated method stub                                                                         EditTextphonenoTest=(EditText) findViewById(R.id.phoneNo);  //獲得文字框對象                                     Stringphoneno=phonenoTest.getText().toString(); //獲得輸入的手機號碼                                     if((phoneno!=null)&&(!"".equals(phoneno.trim()))){                                              Intent intent=newIntent(Intent.ACTION_CALL,Uri.parse("tel:"+phoneno));  //tel:首碼不要寫錯,用於建立一個撥打到電話的意圖                                               startActivity(intent);//發送意圖                                     }                            }                   });    }} 

     具體解釋詳見注釋

 

      ●申請撥號許可權


      由於我們是撥打手機系統中的撥號器,因此我們要申請撥打到電話的權利,修改AndroidManiFest.xml檔案,加入一句:

     <uses-permissionandroid:name="android.permission.CALL_PHONE" />

即可,如下所示:


<?xml version="1.0"encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.sinosoft"    android:versionCode="1"    android:versionName="1.0" >     <uses-sdk android:minSdkVersion="10"/>    <uses-permission android:name="android.permission.CALL_PHONE" />       <application       android:icon="@drawable/ic_launcher"       android:label="@string/app_name" >        <activity           android:name=".HTCMobileActivity"           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> </manifest>

 

     好了,準備工作做完了,開始運行項目,在模擬器裡輸入另一個模擬器的“手機號碼”




     點擊“撥打此號”按鈕時,就會出現用內建撥號器撥打到電話的那一幕!


     這是本人學習的結果,歡迎轉載,歡迎交流,但轉載務必給出本文章的連結地址:http://blog.csdn.net/youqishini/article/details/7366375,謝謝~


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.