当前位置: 移动技术网 > 移动技术>移动开发>Android > Android 实现手机拨打电话的功能

Android 实现手机拨打电话的功能

2019年07月24日  | 移动技术网移动技术  | 我要评论

一部手机最常用的功能就是打电话和发短信了,在android开发中我们如何通过程序拨打电话呢?本文就给出一个用android手机拨打电话的简单的实例。

       下面是开发此实例的具体步骤:

       一、新建一个android工程,命名为phonecalldemo。

       二、设计程序的界面,打开main.xml把内容修改如下:

xml/html代码

<?xml version="1.0" encoding="utf-8"?> 
 
  <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
  android:orientation="vertical" 
 
  android:layout_width="fill_parent" 
 
  android:layout_height="fill_parent" 
 
  > 
 
  <textview 
 
  android:layout_width="fill_parent" 
 
  android:layout_height="wrap_content" 
 
  android:text="please input the phonenumer:" 
 
  /> 
 
  <edittext 
 
  android:id="@+id/et1" 
 
  android:layout_width="fill_parent" 
 
  android:layout_height="wrap_content" 
 
  android:phonenumber="true" 
 
  /> 
 
  <button 
 
  android:id="@+id/bt1" 
 
  android:layout_width="wrap_content" 
 
  android:layout_height="wrap_content" 
 
  android:text="call phone" 
 
  /> 
 
  </linearlayout> 

  三、增加拨打电话的权限,打开androidmanifest.xml,修改代码如下:

xml/html代码

<?xml version="1.0" encoding="utf-8"?> 
 
  <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
 
  package="com.android.test" 
 
  android:versioncode="1" 
 
  android:versionname="1.0"> 
 
  <application android:icon="@drawable/icon" android:label="@string/app_name"> 
 
  <activity android:name=".phonecalldemo" 
 
  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> 
 
  <uses-sdk android:minsdkversion="3" />  
 
  <uses-permission android:name="android.permission.call_phone"> 
 
  </uses-permission> 
 
  </manifest> 

 四、主程序phonecalldemo.java代码如下:

 package com.android.test;import android.app.activity; 
 
  import android.content.intent; 
 
  import android.net.uri; 
 
  import android.os.bundle; 
 
  import android.view.view; 
 
  import android.widget.button; 
 
  import android.widget.edittext; 
 
  import android.widget.toast;
 
 public class phonecalldemo extends activity { 
 
  private button bt; 
 
  private edittext et; 
 
  public void oncreate(bundle savedinstancestate) { 
 
  super.oncreate(savedinstancestate); 
 
  setcontentview(r.layout.main); 
 
  //取得资源 
 
  bt = (button)findviewbyid(r.id.bt1); 
 
  et = (edittext)findviewbyid(r.id.et1); 
 
  //增加事件响应 
 
  bt.setonclicklistener(new button.onclicklistener(){ @override 
 
  public void onclick(view v) { 
 
  //取得输入的电话号码串 
 
  string inputstr = et.gettext().tostring(); 
 
  //如果输入不为空创建打电话的intent 
 
  if(inputstr.trim().length()!=0) 
 
  { 
 
  intent phoneintent = new intent("android.intent.action.call", 
 
  uri.parse("tel:" + inputstr)); 
 
  //启动 
 
  startactivity(phoneintent); 
 
  } 
 
  //否则toast提示一下 
 
  else{ 
 
  toast.maketext(phonecalldemo.this, "不能输入为空", toast.length_long).show(); 
 
  } 
 
  } 
 
  }); 
 
  } 

以上就是android 开发拨打电话的简单示例,后续继续补充相关资料,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网