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)文章
關(guān)于Android 4.4相機預(yù)覽、錄像花屏的問題的解決方法
這篇文章主要介紹了關(guān)于Android 4.4相機預(yù)覽、錄像花屏的問題的解決方法,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2016-12-12
Android 錢包支付之輸入支付密碼的實現(xiàn)步驟
這篇文章主要介紹了Android 錢包支付之輸入支付密碼,需要的朋友可以參考下2018-04-04
Android Back鍵點擊兩次退出應(yīng)用詳解及實現(xiàn)方法總結(jié)
這篇文章主要介紹了Android Back鍵點擊兩次退出應(yīng)用詳解及實現(xiàn)方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2016-10-10
Android如何使用圓形揭露動畫巧妙地隱藏或顯示View詳解
Android開發(fā)中會遇到不少顯示和隱藏的問題,下面這篇文章主要給大家介紹了關(guān)于Android如何使用圓形揭露動畫巧妙地隱藏或顯示View的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
android開發(fā)教程之listview顯示sqlite數(shù)據(jù)
這篇文章主要介紹了android使用listview顯示sqlite數(shù)據(jù)的方法,需要的朋友可以參考下2014-03-03

