码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-11-26 18:53:05      阅读:341      评论:0      收藏:0      [点我收藏+]

标签:

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

下面我们来做个小例子

首先需要在AndroidManifest.xml中添加一个使用权限,这个容易忘哈哈。

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

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="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();
        }
    }
}

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

界面截图如下:

技术分享

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

截图如下:

技术分享

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

技术分享

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

技术分享

 

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

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

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

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

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

标签:

原文地址:http://www.cnblogs.com/chengyujia/p/4998446.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!