Android開發(fā)中實現(xiàn)發(fā)送短信的小程序示例

上圖為代碼結(jié)構(gòu)圖。
現(xiàn)在我們看下具體的代碼。
Send.java
package cn.com.sms.send;
import java.util.ArrayList;
import java.util.Iterator;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Send extends Activity {
private String message;
private String number ;
private EditText editText;
private EditText editText2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText) this.findViewById(R.id.number);
editText2 = (EditText)this.findViewById(R.id.message);
Button button = (Button)this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
number = editText.getText().toString();
message = editText2.getText().toString();
// 在LogCat中可以查看到number和message的相關信息
Log.i("number", number);
Log.i("message", message);
/*獲取系統(tǒng)默認的信息管理器,一定要注意的是SmsManager是android.telephony.SmsManager;這和
*我們使用的版本有關,在 Android 2.0 以前 應該使用 android.telephony.gsm.SmsManager
*Android 2.0 之后的版本應該用 android.telephony.SmsManager。
*/
SmsManager smsManager = SmsManager.getDefault();
/*PendingIntent.getBroadcast返回一個用于廣播的PendingIntent對象,類似于調(diào)用Content.sendBroadcast();
*/
PendingIntent paIntent = PendingIntent.getBroadcast(Send.this, 0, new Intent("SMS_SENT"), 0);
PendingIntent deliveryIntent = PendingIntent.getBroadcast(Send.this, 0, new Intent("SMS_DELIVERED"), 0);
// smsManager.divideMessage有些時候短信如果超過了字數(shù),我們就需要這個方法來幫我們拆分短信內(nèi)容。
ArrayList<String> smses = smsManager.divideMessage(message);
Iterator<String> iterator = smses.iterator();
while(iterator.hasNext()){
String temp = iterator.next();
//發(fā)送短信
smsManager.sendTextMessage(number, null, temp, paIntent, deliveryIntent);
}
// 彈出一個浮動框顯示提示內(nèi)容,Toast.LENGTH_LONG代表浮動框顯示時間的長短
Toast.makeText(Send.this, "短信發(fā)送完成", Toast.LENGTH_LONG).show();
}
});
}
}
main.xml
<?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="歡迎使用短信發(fā)送器,請輸入電話號碼" /> <EditText android:id="@+id/number" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="這里輸入電話號碼" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="歡迎使用短信發(fā)送器,請輸入短信內(nèi)容" /> <EditText android:id="@+id/message" android:layout_width="fill_parent" android:layout_height="wrap_content" android:minLines="3" android:hint="這里輸入短信內(nèi)容" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="send" /> </LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.com.sms.send"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Send"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
最終效果圖為:

和打電話小程序一樣,這里也需要開啟兩個AVD才能進行功能測試。
碎碎念:
發(fā)短信應用的主要的類就是SmsManager。 在 Android 2.0 以前 應該使用 android.telephony.gsm.SmsManager
之后應該用 android.telephony.SmsManager;
SmsManager smsManager = SmsManager.getDefault();
意思為獲取系統(tǒng)默認的信息管理器
smsManager.sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent)
-- destinationAddress:目標電話號碼
-- scAddress:服務商的短信中心號碼(例如中國移動的短信中心號碼),測試可以不填。
-- text: 短信內(nèi)容
-- sentIntent:發(fā)送 -->中國移動 --> 中國移動發(fā)送失敗 --> 返回發(fā)送成功或失敗信號 --> 后續(xù)處理 即,這個意圖包裝了短信發(fā)送狀態(tài)的信息
-- deliveryIntent: 發(fā)送 -->中國移動 --> 中國移動發(fā)送成功 --> 返回對方是否收到這個信息 --> 后續(xù)處理 即:這個意圖包裝了短信是否被對方收到的狀態(tài)信息(供應商已經(jīng)發(fā)送成功,但是對方?jīng)]有收到)。
public static PendingIntent getBroadcast (Context context, int requestCode, Intent intent, int flags)
返回一個用于廣播的PendingIntent,類似于調(diào)用Context.sendBroadcast()函數(shù)
requestCode 暫時不用
intent 是用于廣播的intent
flag 有:FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT 用于設置新建的PendingIntent是使用一次、如無則不創(chuàng)建、取消當前、更新當前等屬性。
此外,我們還要在AndroidManifest.xml中聲明短信發(fā)送權限。
<uses-permission android:name="android.permission.SEND_SMS"/>
有的時候,我們兩個AVD進行模擬發(fā)短信時,會發(fā)現(xiàn)有時候該程序無法正常使用。系統(tǒng)會提示我們NO DNS servers found,找不到DNS服務。這種情況一般是由于你的電腦沒有聯(lián)入網(wǎng)絡的原因造成的。
發(fā)送短信:
SmsManager smsMgr = SmsManager.getDefault(); smsMgr.sendTextMessage(address, null, message, null, null);
顯示寫短信界面:
Uri smsToUri = Uri.parse("smsto://10086");
Intent mIntent = new Intent( android.content.Intent.ACTION_SENDTO, smsToUri );
startActivity( mIntent );
發(fā)送電子郵件:
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_EMAIL, address);
i.putExtra(Intent.EXTRA_SUBJECT, filename);
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + filename)); ;
i.setType("text/csv");
startActivity(Intent.createChooser(i, "EMail File"));
升級版:
該代碼為其添加了廣播接收者的監(jiān)聽。詳細代碼如下
package cn.com.sms.send;
import java.util.ArrayList;
import java.util.Iterator;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Send extends Activity {
private String message;
private String number ;
private EditText editText;
private EditText editText2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText) this.findViewById(R.id.number);
editText2 = (EditText)this.findViewById(R.id.message);
Button button = (Button)this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
number = editText.getText().toString();
message = editText2.getText().toString();
// 在LogCat中可以查看到number和message的相關信息
Log.i("number", number);
Log.i("message", message);
/*獲取系統(tǒng)默認的信息管理器,一定要注意的是SmsManager是android.telephony.SmsManager;這和
*我們使用的版本有關,在 Android 2.0 以前 應該使用 android.telephony.gsm.SmsManager
*Android 2.0 之后的版本應該用 android.telephony.SmsManager。
*/
SmsManager smsManager = SmsManager.getDefault();
/*PendingIntent.getBroadcast返回一個用于廣播的PendingIntent對象,類似于調(diào)用Content.sendBroadcast();
*/
PendingIntent paIntent = PendingIntent.getBroadcast(Send.this, 0, new Intent("SMS_SENT2"), 0);
PendingIntent deliveryIntent = PendingIntent.getBroadcast(Send.this, 0, new Intent("SMS_DELIVERED2"), 0);
// 注冊一個BroadcastReceiver,當有匹配它的IntentFilter的Intent出現(xiàn)時,該方法會被觸發(fā)
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
int resultCode = getResultCode();
switch(resultCode){
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "信息發(fā)送成功了哦、", Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(getBaseContext(), "信息發(fā)送失敗了哦、", Toast.LENGTH_LONG).show();
}
}
}, new IntentFilter("SMS_SENT2"));
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(getBaseContext(), "deliveryIntent", Toast.LENGTH_LONG).show();
Log.i("短信接收人是否查看信息", "看了");
}
}, new IntentFilter("SMS_DELIVERED2"));
// smsManager.divideMessage有些時候短信如果超過了字數(shù),我們就需要這個方法來幫我們拆分短信內(nèi)容。
ArrayList<String> smses = smsManager.divideMessage(message);
Iterator<String> iterator = smses.iterator();
while(iterator.hasNext()){
String temp = iterator.next();
//發(fā)送短信
smsManager.sendTextMessage(number, null, temp, paIntent, deliveryIntent);
}
// 彈出一個浮動框顯示提示內(nèi)容,Toast.LENGTH_LONG代表浮動框顯示時間的長短
Toast.makeText(Send.this, "短信發(fā)送完成", Toast.LENGTH_LONG).show();
}
});
}
}
main.xml與AndroidManifest.xml和前面的代碼一樣。
registerReceiver()用于注冊廣播接受者。該方法在Content中這樣定義的。
public abstract Intent registerReceiver(BroadcastReceiver receiver,IntentFilter filter);系統(tǒng)如果查詢到滿足filter的廣播,便會教給receiver,讓其處理。一般都是在其onReceive()方法中處理。
如果不是在代碼中主動通過registerReceiver()進行注冊,那么就要從AndroidManifest.xml進行配置,代碼如下
<receiver android:name="類名"> <intent-filter> <action android:name="接收者中Intent參數(shù)的action屬性" /> </intent-filter> </receiver>
這里需要注意,在配置文件中activity標簽和receiver標簽是平級的。
在模擬器中發(fā)送中文會接收方出現(xiàn)亂碼的問題,但是在真機中,就不會出現(xiàn)亂碼的情況了。所以開發(fā)者只需要正常開發(fā)短信功能,不需要編碼轉(zhuǎn)換。
- Android Studio EditText點擊圖標清除文本內(nèi)容的實例解析
- android編程實現(xiàn)添加文本內(nèi)容到sqlite表中的方法
- Android應用中使用及實現(xiàn)系統(tǒng)“分享”接口實例
- Android 第三方登錄、分享(ShareSDK、友盟)
- 分享一個Android設置圓形圖片的特別方法
- Android超實用的Toast提示框優(yōu)化分享
- Android實現(xiàn)第三方授權登錄、分享以及獲取用戶資料
- Android藍牙通信聊天實現(xiàn)發(fā)送和接受功能
- Android編程實現(xiàn)QQ表情的發(fā)送和接收完整實例(附源碼)
- Android廣播接收機制詳細介紹(附短信接收實現(xiàn))
- Android短信接收監(jiān)聽、自動回復短信操作例子
- 簡述Android中實現(xiàn)APP文本內(nèi)容的分享發(fā)送與接收方法
相關文章
Android中button實現(xiàn)onclicklistener事件的兩種方式
本文介紹下Android中button實現(xiàn)onclicklistener事件的兩種方法,感興趣的朋友可以參考下2013-04-04
Android?內(nèi)存優(yōu)化知識點梳理總結(jié)
這篇文章主要介紹了Android?內(nèi)存優(yōu)化知識點梳理總結(jié),Android?操作系統(tǒng)給每個進程都會分配指定額度的內(nèi)存空間,App?使用內(nèi)存來進行快速的文件訪問交互,長時間如此便需要優(yōu)化策略,文章分享優(yōu)化知識點總結(jié),需要的朋友可以參考一下2022-06-06
Android DatePicker和DatePickerDialog基本用法示例
這篇文章主要介紹了Android DatePicker和DatePickerDialog基本用法,實例分析了DatePicker和DatePickerDialog控件針對手機時間設置的相關技巧,需要的朋友可以參考下2016-06-06
Android開啟閃光燈的方法 Android打開手電筒功能
這篇文章主要為大家詳細介紹了Android開啟閃光燈的方法,Android打開手電筒功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
android listview優(yōu)化幾種寫法詳細介紹
這篇文章只是總結(jié)下getView里面優(yōu)化視圖的幾種寫法,需要的朋友可以參考下2012-11-11

