星空网 > 软件开发 > 操作系统

Android APP 两种用程序拨号的方式

想在APP中添加一个拨号功能该怎样做呢?Android提供了两种方式,一种是ACTION_CALL方式直接拨打,另一种是ACTION_DIAL方式打开系统的拨号界面。

下面我们来做个小例子

首先需要在AndroidManifest.

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

然后搭一个简单的界面测试一下,下面是布局文件代码

<??><LinearLayout ="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical" >  <TextView    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:text="请输入要拨打的号码:" />  <EditText    android:id="@+id/etPhone"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:inputType="phone" />  <Button    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:onClick="onClickActionCall"    android:text="ACTION_CALL方式直接拨打" />  <Button    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:onClick="onClickActionDial"    android:text="ACTION_DIAL方式打开拨号界面" /></LinearLayout>

下面是对应的Activity代码:

package chengyujia.androidtest;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.widget.EditText;import android.widget.Toast;public class CallActivity extends Activity {  private EditText etPhone;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_call);    etPhone = (EditText) findViewById(R.id.etPhone);  }  // ACTION_CALL方式拨打电话(直接拨打)  public void onClickActionCall(View v) {    //这里的Intent.ACTION_CALL实际就是一个特定的字符串,    //ACTION_CALL = "android.intent.action.CALL",    //告诉系统我要直接拨号了。    call(Intent.ACTION_CALL);  }  // ACTION_DIAL方式拨打电话(打开拨号界面)  public void onClickActionDial(View v) {    //同理,这里的Intent.ACTION_DIAL也是一个特定的字符串    //ACTION_DIAL = "android.intent.action.DIAL"    //告诉系统我要打开拨号界面,并把要拨的号显示在拨号界面上,由用户决定是否要拨打。    call(Intent.ACTION_DIAL);  }    private void call(String action){    String phone = etPhone.getText().toString();    if(phone!=null&&phone.trim().length()>0){    //这里"tel:"+电话号码 是固定格式,系统一看是以"tel:"开头的,就知道后面应该是电话号码。    Intent intent = new Intent(action, Uri.parse("tel:" + phone.trim()));    startActivity(intent);//调用上面这个intent实现拨号    }else{      Toast.makeText(this, "电话号码不能为空", Toast.LENGTH_LONG).show();    }  }}

下面运行一下,看看效果。

界面截图如下:

Android APP 两种用程序拨号的方式images/loading.gif' data-original="http://images2015.cnblogs.com/blog/343777/201511/343777-20151126170244171-224759516.png" />

我填写了电话号码10086,下面点击第一个按钮“ACTION_CALL方式直接拨打”,

截图如下:

Android APP 两种用程序拨号的方式

发现并没有直接拨出去,而是给了用户一个提示,让用户选择是否真的要拨号,这也是防止有人作恶啊。科技本应该让生活更美好,而不是让生活更糟糕,但不是每个人都这么想的哦,所以不得不防啊。系统做的对,咱继续测试,点击“允许一次”,就开始真正拨号了,截图如下:

Android APP 两种用程序拨号的方式

挂了电话,回到刚才的测试界面,点击第二个按钮“ACTION_DIAL方式打开拨号界面”,下面是点击后的截图:

Android APP 两种用程序拨号的方式

 

这就是系统的拨号界面,同时把要拨的号码也给用户写好了,要不要拨就由用户决定喽。

实际开发中用哪种方式,这个要看具体情况了。好了,关于Android APP 用程序实现拨号功能就写这些吧。

工作不是生活的全部,最后放一个搞笑的段子,乐呵乐呵

菩提老祖将悟空唤至身前:“你已学会长生不老术和七十二变,今日为师欲传授你新的法术。” 悟空道:“是何法术?”菩提老祖道:“看到这天上的云彩了吗?这边有七朵云彩,那边有五朵云彩,一共有几朵?” 悟空答:“十二朵。” 菩提老祖道:“嗯,我要教你的就是云计算。”




原标题:Android APP 两种用程序拨号的方式

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

跨境电商为什么应该选择TikTok网红营销|hotlist:https://www.goluckyvip.com/news/4518.html
笔记本电脑国际快递介绍:https://www.goluckyvip.com/news/4519.html
中外航线涨价,2019年中外航线油轮运费或涨60%-70%!:https://www.goluckyvip.com/news/452.html
寄往比利时的化妆品物流渠道有哪些?:https://www.goluckyvip.com/news/4520.html
2021年海外仓要火!资深大卖揭秘布局海外仓“五要素”和“三部曲”:https://www.goluckyvip.com/news/4521.html
东南亚“致胜秘籍”全拆解,跨境小白也能成功爆单!:https://www.goluckyvip.com/news/4522.html
恐怖游轮2002 恐怖游轮2022:https://www.vstour.cn/a/365178.html
时尚电商平台Meesho拟融资3亿美元!:https://www.kjdsnews.com/a/1836524.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流