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)輸入電話號(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)造出布局如圖
二、授予軟件打電話權(quán)限
在AndroidManifest.xml添加如下代碼
<uses-permission android:name="android.permission.CALL_PHONE"/>
授予軟件打電話權(quán)限,否則打不了電話
三、寫代碼(適用于安卓6.0以下)
1).定義一個(gè)外部類去實(shí)現(xiàn)setOnClickListener所需要的接口類型
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è)類去實(shí)現(xiàn)setOnClickListener所需要的接口類型 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 //所以Length_long可直接寫成1,Length_short可直接寫成0 Toast.makeText(MainActivity.this,"所輸號(hào)碼不能為空",Toast.LENGTH_LONG).show(); return; } //6.進(jìn)行撥打電話 意圖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類型 // uri統(tǒng)一資源標(biāo)識(shí)符 url統(tǒng)一資源定位符 intent.setData(Uri.parse("tel:"+num)); //6.3開啟意圖 startActivity(intent); } } }
2)匿名內(nèi)部類實(shí)現(xiàn)setOnClickListener所需要的接口類型
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); } } }); } }
四、寫代碼(適用于安卓6.0及以上)
在Android6.0及以上平臺(tái),即便已經(jīng)添加了打電話的權(quán)限,運(yùn)行時(shí)依然會(huì)報(bào)錯(cuò)安全異常:權(quán)限被拒絕。 應(yīng)該在應(yīng)用啟動(dòng)時(shí)檢查應(yīng)用是否被授予電話權(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) { //檢查是否獲得打電話權(quán)限 //如果沒(méi)有獲得電話權(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)限,被用戶拒絕,返回true //用戶拒絕權(quán)限,并勾選了don't ask again,返回false //設(shè)備策略禁止擁有該權(quán)限,返回false //提示用戶授權(quán) Toast.makeText(MainActivity.this,"請(qǐng)授予該應(yīng)用電話權(quán)限",Toast.LENGTH_LONG).show(); //跳轉(zhuǎn)到該應(yīng)用設(shè)置界面,幫助用戶授權(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電話撥號(hào)器實(shí)例詳解
- Android簡(jiǎn)易電話撥號(hào)器實(shí)例詳解
- Android開發(fā)之電話撥號(hào)器和短信發(fā)送器實(shí)現(xiàn)方法
- Android開發(fā)之電話撥號(hào)器實(shí)例詳解
- Android學(xué)習(xí)筆記(二)之電話撥號(hào)器
- Android電話撥號(hào)器實(shí)現(xiàn)方法
- Android 2.3 撥號(hào)上網(wǎng)流程從源碼角度進(jìn)行分析
- Android Studio Intent隱式啟動(dòng),發(fā)短信,撥號(hào),打電話,訪問(wèn)網(wǎng)頁(yè)等實(shí)例代碼
- Android編程簡(jiǎn)單實(shí)現(xiàn)撥號(hào)器功能的方法
相關(guān)文章
Android AIDL——進(jìn)程通信機(jī)制詳解
這篇文章主要介紹了Android AIDL——進(jìn)程通信機(jī)制詳解的相關(guān)資料,并附簡(jiǎn)單實(shí)例,和實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-10-10詳解Android壁紙服務(wù)的啟動(dòng)過(guò)程
你有設(shè)置過(guò)手機(jī)的壁紙嗎,你知道壁紙是什么樣的程序它是怎么在后臺(tái)運(yùn)行的嗎?這篇文章主要介紹了詳解Android系統(tǒng)壁紙服務(wù)的啟動(dòng)過(guò)程2021-08-08解決Android從相冊(cè)中獲取圖片出錯(cuò)圖片卻無(wú)法裁剪問(wèn)題的方法
這篇文章主要介紹了解決Android從相冊(cè)中獲取圖片出錯(cuò)圖片卻無(wú)法裁剪問(wèn)題的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-01-01Android編程自定義搜索框?qū)崿F(xiàn)方法【附demo源碼下載】
這篇文章主要介紹了Android編程自定義搜索框?qū)崿F(xiàn)方法,涉及Android界面布局、數(shù)據(jù)加載、事件響應(yīng)等相關(guān)操作技巧,并附帶完整demo源碼供讀者下載參考,需要的朋友可以參考下2017-12-12Android Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器
這篇文章主要為大家詳細(xì)介紹了Android Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07為Android的apk應(yīng)用程序文件加殼以防止反編譯的教程
這篇文章主要介紹了為Android的apk應(yīng)用程序文件加殼以防止反編譯的教程,同時(shí)對(duì)apk程序的解殼操作也有詳細(xì)講解,需要的朋友可以參考下2016-04-04詳解Android中的NestedScrolling機(jī)制帶你玩轉(zhuǎn)嵌套滑動(dòng)
這篇文章主要給大家詳細(xì)解析了Android中的NestedScrolling機(jī)制,通過(guò)介紹該機(jī)制帶你玩轉(zhuǎn)Android中的嵌套滑動(dòng)效果,文中給出了詳細(xì)的示例代碼和介紹,需要的朋友們可以參考學(xué)習(xí),下面來(lái)一起看看吧。2017-05-05Android中Listview點(diǎn)贊功能的實(shí)現(xiàn)
最近一段時(shí)間在研究android方面的知識(shí),利用listview實(shí)現(xiàn)點(diǎn)贊功能,下面小編通過(guò)本文給大家介紹下基本思路,需要的朋友可以參考下2016-11-11Android 動(dòng)畫(View動(dòng)畫,幀動(dòng)畫,屬性動(dòng)畫)詳細(xì)介紹
這篇文章主要介紹了Android View動(dòng)畫、幀動(dòng)畫和屬性動(dòng)畫詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2016-10-10