Android模拟手机拨号器

来源:互联网 发布:单片机和plc哪个贵 编辑:程序博客网 时间:2024/05/04 19:20
一直以来对手机之间的通话都很好奇,拨打号码就能实现两个手机连同,今天就可以实现这个小功能,不然还怎么说自己是研究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文件


[html] view plaincopyprint?
  1. <?xml version="1.0"encoding="utf-8"?>  
  2. <resources>  
  3.    
  4.     <string name="hello">Hello World,HTCMobileActivity!</string>  
  5.     <string name="app_name">HTC拨号器</string>  
  6.     <string name="mobile_name" >请输入手机号</string>  
  7.     <string name="call">拨打此号</string>  
  8. </resources>  
  9. 这个文件主要用来定义字符串和数值  
  10. ●编写main.xml文件  
  11. 在项目的res/layout目录下找到此文件  
  12. <?xml version="1.0"encoding="utf-8"?>  
  13. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  14.     android:layout_width="fill_parent"  
  15.     android:layout_height="fill_parent"  
  16.     android:orientation="vertical" >  
  17.    
  18.   <TextView  
  19.        android:layout_width="fill_parent"  
  20.        android:layout_height="wrap_content"  
  21.        android:text="@string/mobile_name" />  
  22.    
  23.     <EditText   
  24.         android:id="@+id/phoneNo"  
  25.          android:layout_width="fill_parent"  
  26.        android:layout_height="wrap_content"  
  27.         />  
  28.    
  29.     <Button  
  30.         android:id="@+id/cllPhone"  
  31.         android:layout_width="wrap_content"  
  32.         android:layout_height="wrap_content"  
  33.         android:text="@string/call"/>  
  34.    
  35. </LinearLayout>  


      此文件主要设置布局文件,类似于我们的html页面文件,在eclipse中我们可以点击文件编辑区的“Graphical Layout”预览效果:




     ●编写HTCMobileMobileActivity.java文件


[java] view plaincopyprint?
  1. packagecom.sinosoft;  
  2.    
  3. importandroid.app.Activity;  
  4. importandroid.content.Intent;  
  5. importandroid.net.Uri;  
  6. importandroid.os.Bundle;  
  7. importandroid.view.View;  
  8. importandroid.widget.Button;  
  9. import android.widget.EditText;  
  10.    
  11. publicclass HTCMobileActivity extends Activity {  
  12.     /** Called when the activity is firstcreated. */  
  13.     @Override  
  14.     public void onCreate(BundlesavedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.main);  
  19.         Button button=(Button)findViewById(R.id.cllPhone);  
  20.         button.setOnClickListener(newView.OnClickListener() {  
  21.                              
  22.                             public void onClick(Viewv) {  
  23.                                      // TODOAuto-generated method stub  
  24.                                       
  25.                                      EditTextphonenoTest=(EditText) findViewById(R.id.phoneNo);  //获得文本框对象  
  26.                                      Stringphoneno=phonenoTest.getText().toString(); //获得输入的手机号码  
  27.                                      if((phoneno!=null)&&(!"".equals(phoneno.trim()))){  
  28.                                               Intent intent=newIntent(Intent.ACTION_CALL,Uri.parse("tel:"+phoneno));  //tel:前缀不要写错,用于创建一个拨打电话的意图  
  29.                                                startActivity(intent);//发送意图  
  30.                                      }  
  31.                             }  
  32.                    });  
  33.     }  
  34. }  
  35.    


     具体解释详见注释

 

      ●申请拨号权限


      由于我们是拨打手机系统中的拨号器,因此我们要申请拨打电话的权利,修改AndroidManiFest.xml文件,加入一句:

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

即可,如下所示:


[html] view plaincopyprint?
  1. <?xml version="1.0"encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.sinosoft"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.    
  7.     <uses-sdk android:minSdkVersion="10"/>  
  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=".HTCMobileActivity"  
  15.            android:label="@string/app_name" >  
  16.             <intent-filter>  
  17.                 <action android:name="android.intent.action.MAIN" />  
  18.    
  19.                 <category android:name="android.intent.category.LAUNCHER" />  
  20.             </intent-filter>  
  21.         </activity>  
  22.     </application>  
  23.    
  24. </manifest>  


 

     好了,准备工作做完了,开始运行项目,在模拟器里输入另一个模拟器的“手机号码”




     点击“拨打此号”按钮时,就会出现用自带拨号器拨打电话的那一幕!


     这是本人学习的结果,欢迎转载,欢迎交流,但转载务必给出本文章的链接地址:http://blog.csdn.net/youqishini/article/details/7366375,谢谢~


原创粉丝点击