Android電話撥號器

來源:互聯網
上載者:User

  在Android模擬器中開發時,有時需要類比撥打到電話功能,由於模擬器不能直接當做真機使用,所以我們需要再模擬器中類比真機撥打到電話,首先需要建立兩個模擬器,當做兩部Android手機來使用。由於Android系統中已經有了撥打到電話的Activity,因此我們只需要編寫代碼調用即可。具體如下:

  1. 建立如下布局:

    

    對應的布局檔案xml:

    

    

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" > 5  6     <TextView 7         android:id="@+id/textView1" 8         android:layout_width="wrap_content" 9         android:layout_height="wrap_content"10         android:text="@string/phone_tl"11         tools:context=".MainActivity" />12 13     <EditText14         android:id="@+id/editText1"15         android:layout_width="fill_parent"16         android:layout_height="wrap_content"17         android:layout_alignParentLeft="true"18         android:layout_below="@+id/textView1"19         android:layout_marginTop="14dp"20         android:ems="10" >21 22         <requestFocus />23     </EditText>24 25     <Button26         android:layout_width="wrap_content"27         android:layout_height="wrap_content"28         android:layout_alignParentLeft="true"29         android:layout_below="@+id/editText1"30         android:id="@+id/btn_call"31         android:text="@string/button" />32 33 </RelativeLayout>

  2. 編寫代碼檔案:

    

 1 package com.example.phone; 2  3 import android.net.Uri; 4 import android.os.Bundle; 5 import android.app.Activity; 6 import android.content.Intent; 7 import android.view.Menu; 8 import android.view.View; 9 import android.widget.Button;10 import android.widget.EditText;11 12 /**13  * @author fanchangfa14  * 撥打到電話類比15  */16 public class MainActivity extends Activity {17 18     private EditText phone_text;    //擷取用於輸入電話號碼的文字框對象19     private Button btn_call;        //擷取撥打到電話的Button對象20     @Override21     public void onCreate(Bundle savedInstanceState) {22         super.onCreate(savedInstanceState);23         setContentView(R.layout.activity_main);24         25         /*26          *     初始化控制項27          **/28         phone_text = (EditText) this.findViewById(R.id.editText1);29         30         btn_call = (Button) this.findViewById(R.id.btn_call);31         32         btn_call.setOnClickListener(new btn_listener());33     }34     35     //撥打到電話的按鈕單擊事件:36     private final class btn_listener implements View.OnClickListener{37         public void onClick(View v)38         {39             String phone = phone_text.getText().toString();40             41             Intent Int_call = new Intent();42             43             Int_call.setAction("android.intent.action.CALL");44             Int_call.setData(Uri.parse("tel:"+phone));45             46             //使用Intent時,還需要設定其category,不過47             //方法內部會自動為Intent添加類別:android.intent.category.DEFAULT48             49             startActivity(Int_call);50         }51     }52 53     @Override54     public boolean onCreateOptionsMenu(Menu menu) {55         getMenuInflater().inflate(R.menu.activity_main, menu);56         return true;57     }58 }

 

   3. 主要任務完成了,不過此時還不能順利的實現功能,Google為了保護使用者的私人資訊設定了一些許可權,我們開發的時候需要將特定的許可權加入才能正常使用,再此我們需要將撥打到電話的許可權加入到AndroidMainfest.xml檔案中:

  

1     <!-- 添加撥打到電話許可權 -->2     <uses-permission android:name="android.permission.CALL_PHONE" />

  4. 此時運行程式,不過需要兩個模擬器來實現。啟動兩個模擬器:

    

    

    

 

  可以看到,我們所編寫的程式是部署在5556這個模擬器上,另外一個模擬器是5554,現在在我們的程式中輸入5554點擊撥打按鈕,會自動調用系統的撥打到電話Activity,效果如下:

    

 

  這個功能通常是在程式中需要調用撥打到電話程式時使用,例如在開發查看人員資訊時,如果有電話號碼可以直接調用此系統的此Activity進行撥打到電話。

聯繫我們

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