Android實現(xiàn)簡單的撥號器功能
簡易撥號器的制作方法,具體如下
一、布局構(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="請輸入電話號碼:" 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).定義一個外部類去實現(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點擊事件 btn.setOnClickListener(new MyClickListener()); } //4.定義一個類去實現(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,"所輸號碼不能為空",Toast.LENGTH_LONG).show(); return; } //6.進行撥打電話 意圖Intent Intent intent=new Intent();//創(chuàng)建一個意圖 //6.1設(shè)置動作 打XX intent.setAction(Intent.ACTION_CALL);//設(shè)置打的動作 //6.2設(shè)置要撥打的數(shù)據(jù) uri類型 // uri統(tǒng)一資源標(biāo)識符 url統(tǒng)一資源定位符 intent.setData(Uri.parse("tel:"+num)); //6.3開啟意圖 startActivity(intent); } } }
2)匿名內(nèi)部類實現(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點擊事件 // 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,"所輸入號碼不能為空",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及以上平臺,即便已經(jīng)添加了打電話的權(quán)限,運行時依然會報錯安全異常:權(quán)限被拒絕。 應(yīng)該在應(yīng)用啟動時檢查應(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點擊事件 // btn.setOnClickListener(new MyClickListener(){}); btn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //檢查是否獲得打電話權(quán)限 //如果沒有獲得電話權(quán)限 if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE)!= PackageManager.PERMISSION_GRANTED){ if(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.CALL_PHONE)){ //返回值: //app請求過該權(quán)限,被用戶拒絕,返回true //用戶拒絕權(quán)限,并勾選了don't ask again,返回false //設(shè)備策略禁止擁有該權(quán)限,返回false //提示用戶授權(quán) Toast.makeText(MainActivity.this,"請授予該應(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, "所輸入號碼不能為空", Toast.LENGTH_LONG).show(); } else { Intent intent = new Intent(); intent.setAction(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:" + num)); startActivity(intent); } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決Android從相冊中獲取圖片出錯圖片卻無法裁剪問題的方法
這篇文章主要介紹了解決Android從相冊中獲取圖片出錯圖片卻無法裁剪問題的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-01-01Android編程自定義搜索框?qū)崿F(xiàn)方法【附demo源碼下載】
這篇文章主要介紹了Android編程自定義搜索框?qū)崿F(xiàn)方法,涉及Android界面布局、數(shù)據(jù)加載、事件響應(yīng)等相關(guān)操作技巧,并附帶完整demo源碼供讀者下載參考,需要的朋友可以參考下2017-12-12為Android的apk應(yīng)用程序文件加殼以防止反編譯的教程
這篇文章主要介紹了為Android的apk應(yīng)用程序文件加殼以防止反編譯的教程,同時對apk程序的解殼操作也有詳細講解,需要的朋友可以參考下2016-04-04詳解Android中的NestedScrolling機制帶你玩轉(zhuǎn)嵌套滑動
這篇文章主要給大家詳細解析了Android中的NestedScrolling機制,通過介紹該機制帶你玩轉(zhuǎn)Android中的嵌套滑動效果,文中給出了詳細的示例代碼和介紹,需要的朋友們可以參考學(xué)習(xí),下面來一起看看吧。2017-05-05Android 動畫(View動畫,幀動畫,屬性動畫)詳細介紹
這篇文章主要介紹了Android View動畫、幀動畫和屬性動畫詳細介紹的相關(guān)資料,需要的朋友可以參考下2016-10-10