Android實戰(zhàn)教程第四篇之簡單實現(xiàn)短信發(fā)送器
本文實例為大家分享了Android發(fā)短信功能的實現(xiàn)方法,供大家參考,具體內(nèi)容如下
首先配置一個布局:
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:orientation="vertical" > <EditText android:id="@+id/et_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="phone" android:hint="請輸入對方號碼" /> <EditText android:id="@+id/et_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:lines="5" android:hint="請輸入短信內(nèi)容" android:gravity="top" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="發(fā)送" android:onClick="send" /> </LinearLayout>
然后在activity中把發(fā)短信的代碼寫出來:
package com.ydl.smssender; import java.util.ArrayList; //省略導(dǎo)包 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void send(View v){ //拿到用戶輸入的號碼和內(nèi)容 EditText et_phone = (EditText) findViewById(R.id.et_phone); EditText et_content = (EditText) findViewById(R.id.et_content); String phone = et_phone.getText().toString(); String content = et_content.getText().toString(); //1.獲取短信管理器 SmsManager sm = SmsManager.getDefault(); //2.切割短信,把長短信分成若干個小短信 ArrayList<String> smss = sm.divideMessage(content);//an ArrayList of strings that, in order, comprise the original message //3.for循環(huán)把集合中所有短信全部發(fā)出去 for (String string : smss) { sm.sendTextMessage(phone, null, string, null, null);//Send a text based SMS. } } }
發(fā)短信是需要系統(tǒng)權(quán)限的:
效果:
開了兩個模擬器,實現(xiàn)了發(fā)短信功能。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 兩個Fragment之間的跳轉(zhuǎn)和數(shù)據(jù)的傳遞實例詳解
這篇文章主要介紹了Android 兩個Fragment之間的跳轉(zhuǎn)和數(shù)據(jù)的傳遞實例詳解的相關(guān)資料,這里說明實現(xiàn)的思路及實現(xiàn)方法,需要的朋友可以參考下2017-07-07Android AIDL實現(xiàn)兩個APP間的跨進(jìn)程通信實例
這篇文章主要為大家詳細(xì)介紹了Android AIDL實現(xiàn)兩個APP間的跨進(jìn)程通信實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04Android實現(xiàn)簡單的下拉刷新pulltorefresh
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)簡單的下拉刷新pulltorefresh的相關(guān)代碼,具有一定的實用性和操作價值,感興趣的小伙伴們可以參考一下2016-07-07Android頂部工具欄和底部工具欄的簡單實現(xiàn)代碼
Android頂部工具欄和底部工具欄的簡單實現(xiàn)代碼,需要的朋友可以參考一下2013-05-05Android設(shè)置透明狀態(tài)欄和透明導(dǎo)航欄
本文主要介紹了Android設(shè)置透明狀態(tài)欄和透明導(dǎo)航欄的方法。具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03Qt5.12.6配置Android Arm開發(fā)環(huán)境(圖文)
本文主要介紹了Qt5.12.6配置Android Arm開發(fā)環(huán)境,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06Android開發(fā)之TextView控件用法實例總結(jié)
這篇文章主要介紹了Android開發(fā)之TextView控件用法,結(jié)合實例形式總結(jié)分析了TextView控件常用的屬性設(shè)置及使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2016-02-02