Android實(shí)現(xiàn)簡(jiǎn)單的撥號(hào)器功能
簡(jiǎn)易撥號(hào)器的制作方法,具體如下
一、布局構(gòu)造
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請(qǐng)輸入電話(huà)號(hào)碼:"
android:textSize="30sp"
/>
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView" />
<Button
android:id="@+id/dial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText"
android:text="Dial"
android:layout_alignParentRight="true"
android:layout_marginTop="20dp"
android:textSize="20sp"/>
</RelativeLayout>
構(gòu)造出布局如圖

二、授予軟件打電話(huà)權(quán)限
在A(yíng)ndroidManifest.xml添加如下代碼
<uses-permission android:name="android.permission.CALL_PHONE"/>
授予軟件打電話(huà)權(quán)限,否則打不了電話(huà)
三、寫(xiě)代碼(適用于安卓6.0以下)
1).定義一個(gè)外部類(lèi)去實(shí)現(xiàn)setOnClickListener所需要的接口類(lèi)型
package com.example.kim.phonedial;
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;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//1.加載布局
setContentView(R.layout.activity_main);
//3.找到控件 editText 和 Button
et=(EditText)findViewById(R.id.editText);
Button btn=(Button)findViewById(R.id.dial);
//3.設(shè)置Buuton點(diǎn)擊事件
btn.setOnClickListener(new MyClickListener());
}
//4.定義一個(gè)類(lèi)去實(shí)現(xiàn)setOnClickListener所需要的接口類(lèi)型
private class MyClickListener implements View.OnClickListener{
public void onClick(View v){
//5.獲取 editText的文本內(nèi)容
String num=et.getText().toString().trim();
if("".equals(num)){
//Lenth_long 在源代碼中的值為1,Length_short在源代碼中的值為0
//所以L(fǎng)ength_long可直接寫(xiě)成1,Length_short可直接寫(xiě)成0
Toast.makeText(MainActivity.this,"所輸號(hào)碼不能為空",Toast.LENGTH_LONG).show();
return;
}
//6.進(jìn)行撥打電話(huà) 意圖Intent
Intent intent=new Intent();//創(chuàng)建一個(gè)意圖
//6.1設(shè)置動(dòng)作 打XX
intent.setAction(Intent.ACTION_CALL);//設(shè)置打的動(dòng)作
//6.2設(shè)置要撥打的數(shù)據(jù) uri類(lèi)型
// uri統(tǒng)一資源標(biāo)識(shí)符 url統(tǒng)一資源定位符
intent.setData(Uri.parse("tel:"+num));
//6.3開(kāi)啟意圖
startActivity(intent);
}
}
}
2)匿名內(nèi)部類(lèi)實(shí)現(xiàn)setOnClickListener所需要的接口類(lèi)型
package com.example.kim.phonedial;
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;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//1.加載布局
setContentView(R.layout.activity_main);
//3.找到控件 editText 和 Button
et=(EditText)findViewById(R.id.editText);
Button btn=(Button)findViewById(R.id.dial);
//3.設(shè)置Buuton點(diǎn)擊事件
// btn.setOnClickListener(new MyClickListener(){});
btn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
String num=et.getText().toString().trim();
if(num.equals("")){
Toast.makeText(MainActivity.this,"所輸入號(hào)碼不能為空",Toast.LENGTH_LONG).show();
}else{
Intent intent=new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+num));
startActivity(intent);
}
}
});
}
}
四、寫(xiě)代碼(適用于安卓6.0及以上)
在A(yíng)ndroid6.0及以上平臺(tái),即便已經(jīng)添加了打電話(huà)的權(quán)限,運(yùn)行時(shí)依然會(huì)報(bào)錯(cuò)安全異常:權(quán)限被拒絕。 應(yīng)該在應(yīng)用啟動(dòng)時(shí)檢查應(yīng)用是否被授予電話(huà)權(quán)限。
package com.example.kim.phonedial;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//1.加載布局
setContentView(R.layout.activity_main);
//3.找到控件 editText 和 Button
et = (EditText) findViewById(R.id.editText);
Button btn = (Button) findViewById(R.id.dial);
//3.設(shè)置Buuton點(diǎn)擊事件
// btn.setOnClickListener(new MyClickListener(){});
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//檢查是否獲得打電話(huà)權(quán)限
//如果沒(méi)有獲得電話(huà)權(quán)限
if(ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.CALL_PHONE)!= PackageManager.PERMISSION_GRANTED){
if(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.CALL_PHONE)){
//返回值:
//app請(qǐng)求過(guò)該權(quán)限,被用戶(hù)拒絕,返回true
//用戶(hù)拒絕權(quán)限,并勾選了don't ask again,返回false
//設(shè)備策略禁止擁有該權(quán)限,返回false
//提示用戶(hù)授權(quán)
Toast.makeText(MainActivity.this,"請(qǐng)授予該應(yīng)用電話(huà)權(quán)限",Toast.LENGTH_LONG).show();
//跳轉(zhuǎn)到該應(yīng)用設(shè)置界面,幫助用戶(hù)授權(quán)
Intent intent=new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri=Uri.fromParts("package",getPackageName(),null);
intent.setData(uri);
startActivity(intent);
}
}else{
CallPhone();
}
}
});
}
private void CheckPermission(){
}
private void CallPhone() {
String num = et.getText().toString().trim();
if (num.equals("")) {
Toast.makeText(MainActivity.this, "所輸入號(hào)碼不能為空", Toast.LENGTH_LONG).show();
} else {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + num));
startActivity(intent);
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android電話(huà)撥號(hào)器實(shí)例詳解
- Android簡(jiǎn)易電話(huà)撥號(hào)器實(shí)例詳解
- Android開(kāi)發(fā)之電話(huà)撥號(hào)器和短信發(fā)送器實(shí)現(xiàn)方法
- Android開(kāi)發(fā)之電話(huà)撥號(hào)器實(shí)例詳解
- Android學(xué)習(xí)筆記(二)之電話(huà)撥號(hào)器
- Android電話(huà)撥號(hào)器實(shí)現(xiàn)方法
- Android 2.3 撥號(hào)上網(wǎng)流程從源碼角度進(jìn)行分析
- Android Studio Intent隱式啟動(dòng),發(fā)短信,撥號(hào),打電話(huà),訪(fǎng)問(wèn)網(wǎng)頁(yè)等實(shí)例代碼
- Android編程簡(jiǎn)單實(shí)現(xiàn)撥號(hào)器功能的方法
相關(guān)文章
Android?實(shí)現(xiàn)單指滑動(dòng)雙指縮放照片demo及過(guò)程解析
這篇文章主要為大家介紹了Android?實(shí)現(xiàn)單指滑動(dòng)雙指縮放照片demo及過(guò)程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Android如何自定義View實(shí)現(xiàn)橫向的雙水波紋進(jìn)度條
最近有個(gè)需求需要實(shí)現(xiàn)自定義加載進(jìn)度條,于是深入研究了一下,這篇文章主要給大家介紹了關(guān)于A(yíng)ndroid如何自定義View實(shí)現(xiàn)橫向的雙水波紋進(jìn)度條的相關(guān)資料,需要的朋友可以參考下2021-11-11
flutter開(kāi)發(fā)的app項(xiàng)目?打包成web
如果你的Flutter版本低于2.0,請(qǐng)先升級(jí)Flutter版本,創(chuàng)建一個(gè)web文件夾來(lái)存放web相關(guān)的資源,使用HTML渲染器打包,該渲染器提供的打開(kāi)速度最快,并且具有良好的瀏覽器兼容性,使用默認(rèn)設(shè)置進(jìn)行打包,提供的打開(kāi)速度為一般,但依然保持良好的瀏覽器兼容性2024-08-08
SurfaceView播放視頻發(fā)送彈幕并實(shí)現(xiàn)滾動(dòng)歌詞
這篇文章主要為大家詳細(xì)介紹了SurfaceView播放視頻發(fā)送彈幕并實(shí)現(xiàn)滾動(dòng)歌詞,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
Android定制自己的EditText輕松改變底線(xiàn)顏色
這篇文章主要介紹了Android如何定制自己的EditText,如何輕松改變底線(xiàn)顏色,感興趣的小伙伴們可以參考一下2015-12-12

