Android實戰(zhàn)教程第四篇之簡單實現短信發(fā)送器
更新時間:2016年11月10日 11:54:11 作者:楊道龍
這篇文章主要為大家詳細介紹了Android實戰(zhàn)教程第四篇之簡單實現短信發(fā)送器,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Android發(fā)短信功能的實現方法,供大家參考,具體內容如下
首先配置一個布局:
<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="請輸入短信內容" 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; //省略導包 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void send(View v){ //拿到用戶輸入的號碼和內容 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)權限的:
復制代碼 代碼如下:
<uses-permission android:name="android.permission.SEND_SMS"/>
效果:
開了兩個模擬器,實現了發(fā)短信功能。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android 兩個Fragment之間的跳轉和數據的傳遞實例詳解
這篇文章主要介紹了Android 兩個Fragment之間的跳轉和數據的傳遞實例詳解的相關資料,這里說明實現的思路及實現方法,需要的朋友可以參考下2017-07-07Qt5.12.6配置Android Arm開發(fā)環(huán)境(圖文)
本文主要介紹了Qt5.12.6配置Android Arm開發(fā)環(huán)境,文中通過圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-06-06Android開發(fā)之TextView控件用法實例總結
這篇文章主要介紹了Android開發(fā)之TextView控件用法,結合實例形式總結分析了TextView控件常用的屬性設置及使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2016-02-02