Android撥號器的實現

jopen 12年前發布 | 3K 次閱讀 Git Diff Margin 網絡開發

Android自帶了撥號功能和撥號器,但是在很多的應用中,需要在自己的應用中集成撥號的功能,方便客戶直接點擊

就可以完成打電話,所以這樣的調用Android撥號器的功能還是非常有實用價值的,下面我們來介紹一下Android撥號器

的實現。

調用Android自帶的撥號功能其實我們是使用滿足他的意圖對象而實現調用的,下面我們首先來看一下主要的

Activity代碼

</div>

  1. package com.bird.phone;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.net.Uri;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10.   
  11. public class PhoneActivity extends Activity {  
  12.     private EditText text;//尋找空間這種事情只需要做一次就夠了  
  13.     /** Called when the activity is first created. */  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.         text = (EditText) findViewById(R.id.mobile);//得到文本輸入框  
  19.         Button button = (Button) this.findViewById(R.id.button);//根據id尋找控件  
  20.         button.setOnClickListener(new View.OnClickListener() {  
  21.               
  22.             @Override  
  23.             public void onClick(View v) {  
  24.                 String mobileText = text.getText().toString();  
  25.                 Intent intent = new Intent();//創建一個意圖對象,用來激發撥號的Activity  
  26.                 intent.setAction("android.intent.action.CALL");  
  27.                 intent.setData(Uri.parse("tel:"+mobileText));  
  28.                 startActivity(intent);//方法內部會自動添加類別,android.intent.category.DEFAULT  
  29.             }  
  30.         });//添加點擊事件   
  31.     }  
  32.     /* 
  33.     //創建內部類實現回調對象 
  34.     private final class ButtonClick implements View.OnClickListener{ 
  35.  
  36.         @Override 
  37.         public void onClick(View v) { 
  38.              
  39.             String mobileText = text.getText().toString(); 
  40.             Intent intent = new Intent();//創建一個意圖對象,用來激發撥號的Activity 
  41.             intent.setAction("android.intent.action.CALL"); 
  42.             intent.setData(Uri.parse("tel:"+mobileText)); 
  43.             startActivity(intent);//方法內部會自動添加類別,android.intent.category.DEFAULT 
  44.         } 
  45.          
  46.     }*/  
  47. }  
  48. </ol> </div>

    大部分介紹都放在了代碼的注釋上面,其實還是很簡單的。

    然后最主要的就是配置文件了

    </div>

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     package="com.bird.phone"  
    4.     android:versionCode="1"  
    5.     android:versionName="1.0" >  
    6.       
    7.      <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8"/>  
    8.      <uses-permission android:name="android.permission.CALL_PHONE"/>  
    9.        
    10.     <application  
    11.         android:icon="@drawable/ic_launcher"  
    12.         android:label="@string/app_name" >  
    13.         <activity  
    14.             android:name=".PhoneActivity"  
    15.             android:label="@string/app_name" >  
    16.             <intent-filter>  
    17.                 <action android:name="android.intent.action.MAIN" />  
    18.                 <category android:name="android.intent.category.LAUNCHER" />  
    19.             </intent-filter>  
    20.         </activity>  
    21.     </application>  
    22.   
    23. </manifest>  
    24. </ol> </div>
      主要是權限問題
      </div>

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

      </div>

      1.   
      </div>

      </div>

      1. 調用撥號功能需要有權限調用撥號,這個東西會在用戶安裝這個應用的時候提示你是否允許他使用撥號的功能,好,下面就是一個  
      </div>

      </div>

      1. 界面的問題了  
      </div>

      </div>

      1. main.xml頁面文件如下  
      </div>

      </div>

      1. <pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>  
      2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
      3.     android:layout_width="fill_parent"  
      4.     android:layout_height="fill_parent"  
      5.     android:orientation="vertical" >  
      6.   
      7.     <TextView  
      8.         android:layout_width="fill_parent"  
      9.         android:layout_height="wrap_content"  
      10.         android:text="@string/mobile" />  
      11.        
      12.     <EditText   
      13.         android:layout_width="fill_parent"  
      14.         android:layout_height="wrap_content"  
      15.         android:inputType="text"  
      16.         android:id="@+id/mobile"  
      17.        />  
      18.       
      19.     <Button   
      20.         android:layout_width="wrap_content"  
      21.         android:layout_height="wrap_content"  
      22.         android:text="@string/button"  
      23.         android:id="@+id/button"  
      24.         />  
      25. </LinearLayout>  
      26. </ol> </div>
        然后是strings.xml
        </div>

        1. <pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>  
        2. <resources>  
        3.   
        4.     <string name="hello">Hello World, PhoneActivity!</string>  
        5.     <string name="app_name">Phone</string>  
        6.     <string name="mobile">請輸入手機號</string>  
        7.     <string name="button">撥號</string>  
        8. </resources>  
        9. </ol> </div>
          好,至此一個簡單的Android撥號器就完成了,功能雖小,但是使用的地方還是很多的.

         本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
         轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
         本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!