簡(jiǎn)單實(shí)現(xiàn)android短信發(fā)送器
先看看效果圖:

activity_main.xml
<RelativeLayout 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="com.example.sendinfo.MainActivity" >
<TextView
android:id="@+id/tv_input_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/place_input_number"
android:textSize="20px" />
<EditText
android:id="@+id/et_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv_input_number"
android:inputType="phone" >
</EditText>
<TextView
android:id="@+id/tv_input_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@id/et_number"
android:text="請(qǐng)輸入正文"
android:textSize="20px"
android:textColor="#ff2800" />
<EditText
android:lines="5"
android:id="@+id/et_input_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_below="@id/tv_input_content"
android:inputType="textMultiLine" />
<Button
android:id="@+id/bt_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@id/et_input_content"
android:text="發(fā)送" />
</RelativeLayout>
MainActivity.java
package com.example.sendinfo;
import java.util.ArrayList;
import java.util.Iterator;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.telephony.SmsManager;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements OnClickListener {
//獲取數(shù)據(jù)的對(duì)象
private EditText et_number;
private EditText et_content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_number = (EditText) findViewById(R.id.et_number);
et_content = (EditText) findViewById(R.id.et_input_content);
Button bt_send = (Button) findViewById(R.id.bt_send);
bt_send.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_send:
//獲取用戶輸入的數(shù)據(jù)
String content = et_content.getText().toString().trim();
String number = et_number.getText().toString().trim();
if(TextUtils.isEmpty(content) || TextUtils.isEmpty(number)){
Toast.makeText(this, "電話號(hào)碼或者內(nèi)容不能為空", Toast.LENGTH_SHORT).show();
return;
}else{
//發(fā)送短信API :得到一個(gè)信息管理器 實(shí)例
SmsManager smsManager = SmsManager.getDefault();
//短信大于70字符時(shí),則將短信拆分成多條發(fā)送
ArrayList<String> contents = smsManager.divideMessage(content);
for(String string : contents) {
smsManager.sendTextMessage(number, null, string, null, null);
}
}
break;
default:
break;
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)戰(zhàn)教程第四篇之簡(jiǎn)單實(shí)現(xiàn)短信發(fā)送器
- Android基礎(chǔ)開(kāi)發(fā)小案例之短信發(fā)送器
- Android開(kāi)發(fā)之電話撥號(hào)器和短信發(fā)送器實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)短信發(fā)送功能
- Android短信發(fā)送器實(shí)現(xiàn)方法
- Android Mms之:短信發(fā)送流程(圖文詳解)
- 利用smsmanager實(shí)現(xiàn)后臺(tái)發(fā)送短信示例
- Android發(fā)送短信功能代碼
- Android實(shí)現(xiàn)發(fā)送短信功能實(shí)例詳解
- Android使用SmsManager實(shí)現(xiàn)短信發(fā)送功能
相關(guān)文章
Android App中使用AudioManager類來(lái)編寫(xiě)音頻播放器
這篇文章主要介紹了Android App中使用AudioManager類來(lái)編寫(xiě)音樂(lè)播放器的方法,文中舉了一個(gè)簡(jiǎn)單的例子實(shí)現(xiàn)了基礎(chǔ)的播放暫停和靜音等功能,需要的朋友可以參考下2016-04-04
Android系列之Intent傳遞對(duì)象的幾種實(shí)例方法
Android系列之Intent傳遞對(duì)象的幾種實(shí)例方法,需要的朋友可以參考一下2013-05-05
Android動(dòng)態(tài)繪制餅狀圖的示例代碼
這篇文章主要介紹了Android動(dòng)態(tài)繪制餅狀圖的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
Android中實(shí)現(xiàn)淘寶購(gòu)物車RecyclerView或LIstView的嵌套選擇的邏輯
這篇文章主要介紹了Android中實(shí)現(xiàn)淘寶購(gòu)物車RecyclerView或LIstView的嵌套選擇的邏輯,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12
Android獲取系統(tǒng)時(shí)間以及網(wǎng)絡(luò)時(shí)間
這篇文章主要為大家詳細(xì)介紹了Android獲取系統(tǒng)時(shí)間以及網(wǎng)絡(luò)時(shí)間的方法,感興趣的小伙伴們可以參考一下2016-07-07
TextView實(shí)現(xiàn)圖文混合編排的方法
這篇文章主要為大家詳細(xì)介紹了TextView實(shí)現(xiàn)圖文混合編排的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android實(shí)現(xiàn)ImageView陰影和圖層效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)ImageView陰影和圖層效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
Android中超大圖片無(wú)法顯示的問(wèn)題解決
最近在工作中發(fā)現(xiàn)一個(gè)問(wèn)題,在做圖片瀏覽的時(shí)候發(fā)現(xiàn)超大圖圖片無(wú)法顯示,無(wú)奈只能上網(wǎng)找解決方法,后來(lái)通過(guò)測(cè)試找到了解決的方法,下面這篇文章就主要介紹了Android中超大圖無(wú)法顯示的問(wèn)題解決方法,需要的朋友可以參考借鑒。2017-01-01

