Android 實(shí)現(xiàn)手機(jī)撥打電話的功能
一部手機(jī)最常用的功能就是打電話和發(fā)短信了,在Android開發(fā)中我們?nèi)绾瓮ㄟ^程序撥打電話呢?本文就給出一個(gè)用Android手機(jī)撥打電話的簡(jiǎn)單的實(shí)例。
下面是開發(fā)此實(shí)例的具體步驟:
一、新建一個(gè)Android工程,命名為phoneCallDemo。
二、設(shè)計(jì)程序的界面,打開main.xml把內(nèi)容修改如下:
XML/HTML代碼
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Please input the phoneNumer:" /> <EditText android:id="@+id/et1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:phoneNumber="true" /> <Button android:id="@+id/bt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Call Phone" /> </LinearLayout>
三、增加撥打電話的權(quán)限,打開AndroidManifest.xml,修改代碼如下:
XML/HTML代碼
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.test" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".PhoneCallDemo" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="3" /> <uses-permission android:name="android.permission.CALL_PHONE"> </uses-permission> </manifest>
四、主程序phoneCallDemo.java代碼如下:
package com.android.test;import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class PhoneCallDemo extends Activity {
private Button bt;
private EditText et;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//取得資源
bt = (Button)findViewById(R.id.bt1);
et = (EditText)findViewById(R.id.et1);
//增加事件響應(yīng)
bt.setOnClickListener(new Button.OnClickListener(){ @Override
public void onClick(View v) {
//取得輸入的電話號(hào)碼串
String inputStr = et.getText().toString();
//如果輸入不為空創(chuàng)建打電話的Intent
if(inputStr.trim().length()!=0)
{
Intent phoneIntent = new Intent("android.intent.action.CALL",
Uri.parse("tel:" + inputStr));
//啟動(dòng)
startActivity(phoneIntent);
}
//否則Toast提示一下
else{
Toast.makeText(PhoneCallDemo.this, "不能輸入為空", Toast.LENGTH_LONG).show();
}
}
});
}
以上就是Android 開發(fā)撥打電話的簡(jiǎn)單示例,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料,謝謝大家對(duì)本站的支持!
相關(guān)文章
Android沉浸式狀態(tài)欄微技巧(帶你真正理解沉浸式模式)
因?yàn)锳ndroid官方從來沒有給出過沉浸式狀態(tài)欄這樣的命名,只有沉浸式模式(Immersive Mode)這種說法.下面通過本文給大家介紹Android沉浸式狀態(tài)欄微技巧,需要的朋友參考下2016-12-12
Android通過LIstView顯示文件列表的兩種方法介紹
過ListView顯示SD卡中的文件列表一共有兩種方法,一是:通過繼承ListActivity顯示;二是:利用BaseAdapter顯示,具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下哈2013-06-06
Android中ConstraintLayout約束布局的最全詳細(xì)解析
ConstraintLayout是Google在Google?I/O?2016大會(huì)上發(fā)布的一種新的布局容器(ViewGroup),它支持以靈活的方式來放置子控件和調(diào)整子控件的大小,下面這篇文章主要給大家介紹了關(guān)于Android中ConstraintLayout約束布局詳細(xì)解析的相關(guān)資料,需要的朋友可以參考下2022-08-08
Android WebView線性進(jìn)度條實(shí)例詳解
這篇文章主要介紹了Android WebView線性進(jìn)度條實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2016-01-01
Android WebView與JS交互全面詳解(小結(jié))
本篇文章主要介紹了Android WebView與JS交互全面詳解(小結(jié)),實(shí)現(xiàn)了Android客戶端與Web網(wǎng)頁交互,具有一定的參考價(jià)值,有興趣的可以了解一下2017-11-11
Android GPS獲取當(dāng)前經(jīng)緯度坐標(biāo)
這篇文章主要為大家詳細(xì)介紹了Android GPS獲取當(dāng)前經(jīng)緯度坐標(biāo),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
Android基于OkHttp實(shí)現(xiàn)下載和上傳圖片
這篇文章主要為大家詳細(xì)介紹了Android基于OkHttp實(shí)現(xiàn)下載和上傳圖片功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
Android Studio打包.so庫到apk中實(shí)例詳解
這篇文章主要介紹了Android Studio打包.so庫到apk中實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04

