發送和寫入短信java
在某些場合,咱們須要發送短信,並將短信寫入數據源中,這時咱們就須要瞭解一下發送短信機制和寫入短信機制。android
咱們將試圖發送一條短信到指定的地址,同時將短信的內容寫入到短信數據源中,待短信發送成功後,咱們告知用戶發送成功,待對方接收到短信後,咱們告知用戶對方接收成功。app
要實現這些功能,咱們須要瞭解如下幾個重點內容:ide
1.使用android.telephony.SmsManager的API發送短信佈局
2.使用ContentProvider機制對「content://sms/sent」這個URI進行寫入操做測試
3.註冊「SENT_SMS_ACTION」這個廣播地址,待短信發送成功後接收到這條廣播this
4.註冊「DELIVERED_SMS_ACTION」這個廣播地址,待對方接收到短信後接收到這條廣播spa
下面咱們就用代碼實現這些功能,建立一個名爲SMSActivity的Activity,以下:.net
package com.scott.provider; import java.util.List; import android.app.Activity; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; import android.os.Bundle; import android.telephony.SmsManager; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class SMSActivity extends Activity { private SendReceiver sendReceiver = new SendReceiver(); private DeliverReceiver deliverReceiver = new DeliverReceiver(); private EditText address; private EditText body; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sms); address = (EditText) findViewById(R.id.address); body = (EditText) findViewById(R.id.body); //註冊發送成功的廣播 registerReceiver(sendReceiver, new IntentFilter("SENT_SMS_ACTION")); //註冊接收成功的廣播 registerReceiver(deliverReceiver, new IntentFilter("DELIVERED_SMS_ACTION")); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(sendReceiver); unregisterReceiver(deliverReceiver); } public void sendSMS(View view) { String address = this.address.getText().toString(); String body = this.body.getText().toString(); //android.telephony.SmsManager, not [android.telephony.gsm.SmsManager] SmsManager smsManager = SmsManager.getDefault(); //短信發送成功或失敗後會產生一條SENT_SMS_ACTION的廣播 PendingIntent sendIntent = PendingIntent.getBroadcast(this, 0, new Intent("SENT_SMS_ACTION"), 0); //接收方成功收到短信後,發送方會產生一條DELIVERED_SMS_ACTION廣播 PendingIntent deliveryIntent = PendingIntent.getBroadcast(this, 0, new Intent("DELIVERED_SMS_ACTION"), 0); if (body.length() > 70) { //若是字數超過70,需拆分紅多條短信發送 List<String> msgs = smsManager.divideMessage(body); for (String msg : msgs) { smsManager.sendTextMessage(address, null, msg, sendIntent, deliveryIntent); } } else { smsManager.sendTextMessage(address, null, body, sendIntent, deliveryIntent); } //寫入到短信數據源 ContentValues values = new ContentValues(); values.put("address",address); //發送地址 values.put("body", body); //消息內容 values.put("date", System.currentTimeMillis()); //建立時間 values.put("read", 0); //0:未讀;1:已讀 values.put("type", 2); //1:接收;2:發送 getContentResolver().insert(Uri.parse("content://sms/sent"), values); //插入數據 } private class SendReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(context, "Sent Successfully.", Toast.LENGTH_SHORT).show(); break; default: Toast.makeText(context, "Failed to Send.", Toast.LENGTH_SHORT).show(); } } } /** * 發送方的短信發送到對方手機上以後,對方手機會返回給運營商一個信號, * 運營商再把這個信號發給發送方,發送方此時可確認對方接收成功 * 模擬器不支持,真機上需等待片刻 * @author user * */ private class DeliverReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Delivered Successfully.", Toast.LENGTH_SHORT).show(); } } }
佈局文件以下:code
<?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="address"/> <EditText android:id="@+id/address" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="body"/> <EditText android:id="@+id/body" android:layout_width="fill_parent" android:layout_height="150dp" android:gravity="top"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="sendSMS" android:onClick="sendSMS"/> </LinearLayout>
須要注意的是,這個過程要聲明發送短信的權限和寫入短信的權限,咱們在AndroidManifest.xml的聲明以下:
<!-- 發送短消息 --> <uses-permission android:name="android.permission.SEND_SMS"/> <!-- 寫入短消息 --> <uses-permission android:name="android.permission.WRITE_SMS" />
而後,運行該程序,咱們讓Jack給Lisa發送一條短信,看看結果如何:
看來咱們的操做成功了,到底Jack能不能泡到Lisa呢,朋友們,發揮大家的想象力吧。
最後須要注意的一件事,代碼裏也提到過,就是在模擬器測試時,是不支持「接收成功」這個功能的,因此朋友們想要看到「Delivered Successfully」,還必須在真機上試,而且須要耐心等上片刻。感興趣的朋友趕忙試一試吧。