1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > android一键拨号 源代码 android 使用意图(Intent)实现一键拨号实例

android一键拨号 源代码 android 使用意图(Intent)实现一键拨号实例

时间:2023-11-19 19:51:10

相关推荐

android一键拨号 源代码 android 使用意图(Intent)实现一键拨号实例

本文打算实现具有一个一键拨号功能的 APP

1.布局文件activity_main.xml

xmlns:tools="/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context="cn.sehzh.intenttester.MainActivity" >

android:id="@+id/mCallButton"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:layout_centerHorizontal="true"

android:layout_marginTop="26dp"

android:text="Call" />

2.MainActivity

package cn.sehzh.intenttester;

import android.app.Activity;

import android.content.Intent;

import .Uri;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class MainActivity extends Activity {

private Button mBtn;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mBtn = (Button) findViewById(R.id.mCallButton);

mBtn.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

dialPhoneNumber("110");

}

});

}

private void dialPhoneNumber(String phoneNumber) {

Intent intent = new Intent(Intent.ACTION_CALL);

intent.setData(Uri.parse("tel:" + phoneNumber));

if (intent.resolveActivity(getPackageManager()) != null) {

startActivity(intent);

}

}

}3.运行效果

4.注意

Intent intent = new Intent(Intent.ACTION_CALL);中的ACTION_CALL如果写为ACTION_DIAL则会跳出带键盘拨号界面,而不是直接拨打电话;

需要以下权限

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。