Android開發(fā)之電話撥號(hào)器和短信發(fā)送器實(shí)現(xiàn)方法
本文實(shí)例講述了Android開發(fā)之電話撥號(hào)器和短信發(fā)送器實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
電話撥號(hào)器
實(shí)現(xiàn)原理:用戶輸入電話號(hào)碼,當(dāng)點(diǎn)擊撥打的時(shí)候,由監(jiān)聽對(duì)象捕獲,監(jiān)聽對(duì)象通過文本控件獲取到用戶輸入的電話號(hào)碼,由于系統(tǒng)已經(jīng)實(shí)現(xiàn)了電話撥號(hào)功能,所以我們只需要調(diào)用這個(gè)功能就可以了。
步驟:
1.界面布局
2.編寫Activity
3.使用意圖過濾器激活電話撥號(hào)功能
4.添加電話服務(wù)權(quán)限(用手機(jī)的電話服務(wù),要在清單文件AndroidManifest.xml中添加電話服務(wù)權(quán)限)
如圖所示這三個(gè)控件是垂直擺放的,所以要使用線性布局來擱置顯示控件
效果圖:
界面布局:
<?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="@string/Mobile" /> <!--文本框按鈕--> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/moblie" /> <!--撥號(hào)按鈕 --> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" android:id="@+id/button" /> </LinearLayout>
Activity:
package cn.test.phone; 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; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //根據(jù)控件的id查找到按鈕控件 Button button =(Button)this.findViewById(R.id.button); button.setOnClickListener(new ButtonClickLister()); //點(diǎn)擊事件的處理對(duì)象 } //監(jiān)聽對(duì)象實(shí)現(xiàn)撥打功能 private class ButtonClickLister implements View.OnClickListener{ public void onClick(View v){ EditText mobileText=(EditText)findViewById(R.id.moblie); String moblie=mobileText.getText().toString(); //獲取到用戶輸入的時(shí)間 Intent intent =new Intent(); intent.setAction("android.intent.action.CALL"); intent.setData(Uri.parse("tel:"+moblie)); //根據(jù)意圖過濾器參數(shù)激活電話撥號(hào)功能 startActivity(intent); } } }
添加電話服務(wù)權(quán)限:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.itcast.action" android:versionCode="1" android:versionName="1.0"> 略.... <uses-sdk android:minSdkVersion=“6" /> <!-- 電話服務(wù)權(quán)限 --> <uses-permission android:name="android.permission.CALL_PHONE"/> </manifest>
短信發(fā)送器
短信發(fā)送器和電話撥號(hào)器步驟差不多,需要注意的是當(dāng)獲取到短信內(nèi)容時(shí),如果短信內(nèi)容非常多需要對(duì)短信內(nèi)容進(jìn)行拆分,拆分后存到集合里,對(duì)短信內(nèi)容多條發(fā)送
效果圖:
界面布局:
<?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="@string/moblie" /> <!--文本框按鈕--> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/moblie" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/content" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:minLines="3" android:id="@+id/content" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" android:id="@+id/button" /> </LinearLayout>
Activity:
package cn.test.sms; import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.telephony.SmsManager; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button =(Button)this.findViewById(R.id.button); button.setOnClickListener(new ButtonClickLister()); //點(diǎn)擊事件的處理對(duì)象 } //監(jiān)聽對(duì)象實(shí)現(xiàn)撥打功能 private class ButtonClickLister implements View.OnClickListener{ public void onClick(View v){ EditText moblieText=(EditText)findViewById(R.id.moblie); EditText contentText=(EditText)findViewById(R.id.content); String moble =moblieText.getText().toString();//獲取電話號(hào) String content =contentText.getText().toString();//獲取短信內(nèi)容 SmsManager smsManager=SmsManager.getDefault();//獲得短信管理器 ArrayList<String> texts=smsManager.divideMessage(content);//對(duì)短信內(nèi)容進(jìn)行拆分 for(String text:texts){ smsManager.sendTextMessage(moble, null, text, null, null); //短信發(fā)送 } //采用吐西方式提示用戶發(fā)送成功 Toast.makeText(getApplicationContext(), R.string.success, 1).show(); } } }
添加短信服務(wù)權(quán)限:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.itcast.action" android:versionCode="1" android:versionName="1.0"> 略.... <uses-sdk android:minSdkVersion="8" /> <!-- 短信服務(wù)權(quán)限 --> <uses-permission android:name="android.permission.SEND_SMS" /> </manifest>
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android編程判斷橫屏、豎屏及設(shè)置橫豎屏的方法
這篇文章主要介紹了Android編程判斷橫屏、豎屏及設(shè)置橫豎屏的方法,結(jié)合實(shí)例形式分析了Android針對(duì)橫豎屏的判斷、計(jì)算、設(shè)置等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01Android的App啟動(dòng)時(shí)白屏的問題解決辦法
這篇文章主要介紹了Android的App啟動(dòng)時(shí)白屏的問題相關(guān)資料,在App啟動(dòng)的第一次的時(shí)候白屏?xí)欢螘r(shí)間,這里提供了解決辦法,需要的朋友可以參考下2017-08-08詳解Android應(yīng)用中preference首選項(xiàng)的編寫方法
這篇文章主要介紹了Android應(yīng)用中preference首選項(xiàng)的編寫方法,或許Apple將其翻譯為'偏好設(shè)置'更直觀些,即用戶對(duì)應(yīng)用的一些個(gè)性化調(diào)整菜單,需要的朋友可以參考下2016-04-04用Flutter做桌上彈球(繪圖(Canvas&CustomPaint)API)
這篇文章主要介紹了用Flutter做桌上彈球 聊聊繪圖(Canvas&CustomPaint)API,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07Android自定義View實(shí)現(xiàn)隨機(jī)驗(yàn)證碼
這篇文章主要介紹了Android自定義View實(shí)現(xiàn)隨機(jī)驗(yàn)證碼的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-07-07Android實(shí)戰(zhàn)教程第九篇之短信高效備份
這篇文章主要為大家詳細(xì)介紹了Android實(shí)戰(zhàn)教程第九篇之短信高效備份,利用xml序列化器備份短信,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Flutter實(shí)現(xiàn)自定義篩選框的示例代碼
本文主要介紹了Flutter實(shí)現(xiàn)自定義篩選框的示例代碼,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07