Android电话拨号器_06
在Android模拟器中开发时,有时需要模拟拨打电话功能,由于模拟器不能直接当做真机使用,所以我们需要再模拟器中模拟真机拨打电话,首先需要创建两个模拟器,当做两部Android手机来使用。由于Android系统中已经有了拨打电话的Activity,因此我们只需要编写代码调用即可。具体如下:
1. 建立如下布局:
对应的布局文件xml:

1 <?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:id="@+id/mobile" 16 /> 17 18 <Button 19 android:layout_width="wrap_content" 20 android:layout_height="wrap_content" 21 android:text="@string/button" 22 android:id="@+id/button" 23 /> 24 25 </LinearLayout>
2. 编写代码文件:
1 package FosgeIT.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.Menu; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.widget.Button; 11 import android.widget.EditText; 12 13 /* 14 * 15 * @author YinRQ 16 * 电话模拟器 17 * 2013-07-04 17:43:26 18 */ 19 20 public class MainActivity extends Activity { 21 22 private EditText mobileText;//获取用于输入电话号码的文本框对象 23 private Button button; //获取拨打电话的Button对象 24 25 26 /** Called when the activity is first created. */ 27 @Override 28 public void onCreate(Bundle savedInstanceState) { 29 super.onCreate(savedInstanceState); 30 setContentView(R.layout.main); 31 32 //初始化控件 33 mobileText = (EditText) this.findViewById(R.id.mobile); 34 button = (Button) this.findViewById(R.id.button); 35 button.setOnClickListener(new ButtonClickListener()); 36 } 37 38 //拨打电话的按钮单击事件: 39 private final class ButtonClickListener implements View.OnClickListener{ 40 public void onClick(View v) { 41 String number = mobileText.getText().toString(); 42 Intent intent = new Intent(); 43 intent.setAction("android.intent.action.CALL"); 44 intent.setData(Uri.parse("tel:"+ number)); 45 46 //使用Intent时,还需要设置其category,不过 47 //方法内部会自动为Intent添加类别:android.intent.category.DEFAULT 48 startActivity(intent); 49 } 50 } 51 52 }
3. 主要任务完成了,不过此时还不能顺利的实现功能,谷歌为了保护用户的私人信息设置了一些权限,我们开发的时候需要将特定的权限加入才能正常使用,再此我们需要将拨打电话的权限加入到AndroidMainfest.xml文件中:
1 <!-- 添加拨打电话权限 --> 2 <uses-permission android:name="android.permission.CALL_PHONE" />
4. 此时运行程序,不过需要两个模拟器来实现。启动两个模拟器:
可以看到,我们所编写的程序是部署在5556这个模拟器上,另外一个模拟器是5554,现在在我们的程序中输入5554点击拨打按钮。
这个功能通常是在程序中需要调用拨打电话程序时使用,例如在开发查看人员信息时,如果有电话号码可以直接调用此系统的此Activity进行拨打电话。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?