Android實(shí)戰(zhàn)教程第三篇之簡單實(shí)現(xiàn)撥打電話功能
本文實(shí)例為大家分享了Android打電話功能的實(shí)現(xiàn)代碼,需要一個(gè)文本輸入框輸入號碼,需要一個(gè)按鈕打電話。
本質(zhì):點(diǎn)擊按鈕,調(diào)用系統(tǒng)打電話功能。
xml布局文件代碼::
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="請輸入號碼" /> <EditText android:id="@+id/et_phone" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/bt_call" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="撥打" /> </LinearLayout>
mainactivity中代碼:
package com.ydl.dialer; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //給按鈕設(shè)置點(diǎn)擊偵聽 //1.拿到按鈕對象 Button bt = (Button) findViewById(R.id.bt_call);//Button類是View的子類,向下轉(zhuǎn)型要強(qiáng)轉(zhuǎn)。 //2.設(shè)置偵聽 bt.setOnClickListener(new MyListener()); } class MyListener implements OnClickListener{ //按鈕被點(diǎn)擊時(shí),此方法調(diào)用 @Override public void onClick(View v) { //獲取用戶輸入的號碼 EditText et = (EditText) findViewById(R.id.et_phone); String phone = et.getText().toString(); //我們需要告訴系統(tǒng),我們的動(dòng)作:我要打電話 //創(chuàng)建意圖對象 Intent intent = new Intent(); //把打電話的動(dòng)作ACTION_CALL封裝至意圖對象當(dāng)中 intent.setAction(Intent.ACTION_CALL); //設(shè)置打給誰 intent.setData(Uri.parse("tel:" + phone));//這個(gè)tel:必須要加上,表示我要打電話。否則不會(huì)有打電話功能,由于在打電話清單文件里設(shè)置了這個(gè)“協(xié)議” //把動(dòng)作告訴系統(tǒng),啟動(dòng)系統(tǒng)打電話功能。 startActivity(intent); } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android無需申請權(quán)限撥打電話的兩種方式
- Android撥打電話功能實(shí)例詳解
- 編寫android撥打電話apk應(yīng)用實(shí)例代碼
- Android 實(shí)現(xiàn)手機(jī)撥打電話的功能
- Android中Webview打開網(wǎng)頁的同時(shí)發(fā)送HTTP頭信息方法
- Android實(shí)現(xiàn)將應(yīng)用崩潰信息發(fā)送給開發(fā)者并重啟應(yīng)用的方法
- Android發(fā)送短信功能代碼
- Android Mms之:短信發(fā)送流程(圖文詳解)
- Android實(shí)現(xiàn)發(fā)送短信功能實(shí)例詳解
- android中可以通過兩種方式調(diào)用接口發(fā)送短信
- Android開發(fā)實(shí)現(xiàn)撥打電話與發(fā)送信息的方法分析
相關(guān)文章
Android實(shí)現(xiàn)下拉菜單Spinner效果
這篇文章主要介紹了Android實(shí)現(xiàn)下拉菜單Spinner效果,學(xué)習(xí)Spinner組件的使用方法,非常好用的一款組件,相當(dāng)于從下拉列表中選擇項(xiàng)目,感興趣的小伙伴們可以參考一下2016-04-04解決Kotlin 類在實(shí)現(xiàn)多個(gè)接口,覆寫多個(gè)接口中相同方法沖突的問題
這篇文章主要介紹了解決Kotlin 類在實(shí)現(xiàn)多個(gè)接口,覆寫多個(gè)接口中相同方法沖突的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android仿Keep運(yùn)動(dòng)休息倒計(jì)時(shí)圓形控件
這篇文章主要為大家詳細(xì)介紹了Android仿Keep運(yùn)動(dòng)休息倒計(jì)時(shí)圓形控件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09Android開發(fā)自學(xué)筆記(三):APP布局上
這篇文章主要介紹了Android開發(fā)自學(xué)筆記(三):APP布局上,本文講解了添加ViewGroup、添加ViewGroup、定義string內(nèi)容、添加Button、運(yùn)行程序查看效果等內(nèi)容,需要的朋友可以參考下2015-04-04Android動(dòng)態(tài)自定義圓形進(jìn)度條
這篇文章主要介紹了Android動(dòng)態(tài)自定義圓形進(jìn)度條,需要的朋友可以參考下2017-03-03Android編程圖片操作類定義與用法示例【拍照,相冊選圖及裁剪】
這篇文章主要介紹了Android編程圖片操作類定義與用法,涉及Android拍照,相冊選圖及裁剪等圖片操作功能及權(quán)限控制相關(guān)操作技巧,需要的朋友可以參考下2018-02-02Android編程實(shí)現(xiàn)使用Intent傳輸包含自定義類的ArrayList示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)使用Intent傳輸包含自定義類的ArrayList,涉及Android對象序列化、反序列化、Intent數(shù)據(jù)傳輸?shù)认嚓P(guān)操作技巧,需要的朋友可以參考下2017-08-08android實(shí)現(xiàn)倒計(jì)時(shí)動(dòng)態(tài)圈
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)倒計(jì)時(shí)動(dòng)態(tài)圈,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01Jetpack Compose實(shí)現(xiàn)列表和動(dòng)畫效果詳解
這篇文章主要為大家詳細(xì)講講Jetpack Compose實(shí)現(xiàn)列表和動(dòng)畫效果的方法步驟,文中的代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-06-06