Android實現撥打電話功能

添加權限:android

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

實現:app

package com.example.call_person;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_callOne;
    private Button btn_callTwo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        btn_callOne = (Button) findViewById(R.id.btn_callOne);
        btn_callTwo = (Button) findViewById(R.id.btn_callTwo);

        btn_callOne.setOnClickListener(this);
        btn_callTwo.setOnClickListener(this);
    }

    /**
     * 調用撥號界面
     * @param phone 電話號碼
     */
    private void call(String phone) {
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+phone));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    /**
     * 調用撥號功能
     * @param phone 電話號碼
     */
    @SuppressLint("MissingPermission")
    private void call2(String phone) {
        Intent intent2=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phone));
        startActivity(intent2);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_callOne:

                call("10086");

                break;
            case R.id.btn_callTwo:

                call2("10086");

                break;
        }
    }
}

佈局:ide

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.call_person.MainActivity">

    <Button
        android:id="@+id/btn_callOne"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="只調用撥號界面,不撥出電話" />

    <Button
        android:id="@+id/btn_callTwo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="跳過撥號界面,直接撥打電話" />

</LinearLayout>

 

倆種方式的區別:佈局

1.調用撥號界面:去到了撥號界面,可是實際的撥號是由用戶點擊實現的this

2.調用撥號功能:直接撥打了你所輸入的號碼,因此這種方式對於用戶沒有直接的提示效果,Android推薦使用第一種方式,若是是第二種的話,建議在以前加一個提示,是否撥打號碼,而後肯定後再撥打.net

相關文章
相關標籤/搜索