android调用拨号界面拨打电话功能,Android实现拨打电话功能

添加权限:

实现:

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;

}

}

}

布局:

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">

android:id="@+id/btn_callOne"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="只调用拨号界面,不拨出电话" />

android:id="@+id/btn_callTwo"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="跳过拨号界面,直接拨打电话" />

俩种方式的区别:

1.调用拨号界面:去到了拨号界面,但是实际的拨号是由用户点击实现的

2.调用拨号功能:直接拨打了你所输入的号码,所以这种方式对于用户没有直接的提示效果,Android推荐使用第一种方式,如果是第二种的话,建议在之前加一个提示,是否拨打号码,然后确定后再拨打

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值