您的位置:首页 > 移动开发 > Android开发

Android Studio 之 Intent开发 简单Demo ---- 拨打电话

2019-08-22 21:17 2775 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/qq_41811438/article/details/100024437

布局文件tel_main.xml源码如下:

[code]<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
android:id="@+id/et_tel"
android:inputType="numberDecimal"
android:text="138xxxx4952"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/btn_telephoneCall"
android:text="拨打电话"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.java源码如下:

[code]package com.example.jimmy.demointent3;

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

public class MainActivity extends AppCompatActivity {
private EditText et_tel = null;
private Button btn_telephonyCall = null;

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

et_tel = (EditText)findViewById(R.id.et_tel);
btn_telephonyCall = (Button)findViewById(R.id.btn_telephoneCall);
btn_telephonyCall.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
String strTelephonyNum = et_tel.getText().toString();
Uri uri = Uri.parse("tel:" + strTelephonyNum);
Intent it = new Intent();
it.setAction(Intent.ACTION_DIAL); //跳到打电话的apk上去,准备打电话
//  it.setAction(Intent.ACTION_CALL); //直接就打电话了,我这边测试有点问题,权限设定不对?
it.setData(uri);
MainActivity.this.startActivity(it);
}
});
}
}

权限的给予,在AndroidMainfest.xml中,其源码如下:

[code]<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jimmy.demointent3">

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

 

 

---- The End.

 

 

 

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: