Android--傳送簡訊,並且通知發送方,android--發送方
1、傳送簡訊涉及到許可權,我們需要把許可權加上
2、當我們傳送簡訊時,不管發送是否成功,接收方是否接收到,系統都會發送廣播
3、這時我們註冊廣播去接收一下就可以了
4、布局檔案很簡單,裡面就兩個EditText和一個button
下面上代碼,裡面有注釋:
發送廣播接收器:
package com.example.fanlei.cutnotedemo;import android.app.Activity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.telephony.SmsManager;import android.widget.Toast;/** * 監聽簡訊是否已經發送成功 */public class MySmsSendReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("SMS_SEND")){ switch(getResultCode()) { case Activity.RESULT_OK: Toast.makeText(context,"發送成功",Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: Toast.makeText(context,"發送失敗",Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_NO_SERVICE: break; case SmsManager.RESULT_ERROR_NULL_PDU: break; case SmsManager.RESULT_ERROR_RADIO_OFF: break; } } }}
接收方的廣播
package com.example.fanlei.cutnotedemo;import android.app.Activity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;/** * 監聽對方是否發已經接收到簡訊 */public class MySmsDeliverReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("SMS_DELIVER")){ switch(getResultCode()) { case Activity.RESULT_OK: Toast.makeText(context,"對方已接收",Toast.LENGTH_SHORT).show(); break; case Activity.RESULT_CANCELED: break; } } }}
主函數
package com.example.fanlei.cutnotedemo;import android.app.PendingIntent;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.telephony.SmsManager;import android.view.View;import android.widget.EditText;import android.widget.Toast;import java.util.List;public class MainActivity extends ActionBarActivity { private EditText editText1,editText2; private MySmsSendReceiver mySmsSendReceiver; //傳送簡訊的廣播 private MySmsDeliverReceiver mySmsDeliverReceiver;//對方是否已經接收的廣播 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mySmsSendReceiver = new MySmsSendReceiver(); mySmsDeliverReceiver = new MySmsDeliverReceiver(); editText1 = (EditText) findViewById(R.id.editText1);//手機號 editText2 = (EditText) findViewById(R.id.editText2);//內容 //初始化啟動意圖 Intent sendIntent = new Intent(); sendIntent.setAction("SMS_SEND"); Intent deliverIntent = new Intent(); deliverIntent.setAction("SMS_DELIVER"); //滿足條件後執行Intent final PendingIntent send = PendingIntent.getBroadcast(MainActivity.this,0 ,sendIntent,0); final PendingIntent deliver = PendingIntent.getBroadcast(MainActivity.this,0,deliverIntent,0); //傳送簡訊按鈕 findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String phoneNumber = editText1.getText().toString(); String content = editText2.getText().toString(); if (phoneNumber.equals("")){ Toast.makeText(MainActivity.this,"手機號碼為空白",Toast.LENGTH_SHORT).show(); }else { //獲得簡訊的管理者 SmsManager sm = SmsManager.getDefault(); //簡訊字數限制,如果>70,則需要拆分簡訊發送 if (content.length() > 70){ List<String> contents = sm.divideMessage(content); for (String sms : contents){ sm.sendTextMessage(phoneNumber,null,sms,send,deliver);//傳送簡訊 } }else { sm.sendTextMessage(phoneNumber,null,content,send,deliver);//傳送簡訊 } } } }); //註冊廣播 registerReceiver(mySmsSendReceiver,new IntentFilter("SMS_SEND")); registerReceiver(mySmsDeliverReceiver,new IntentFilter("SMS_DELIVER")); }}
布局檔案
<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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/editText1" android:hint="手機號" android:inputType="phone"/> <EditText android:id="@+id/editText2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/editText1" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:hint="內容"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:text="傳送簡訊" /></RelativeLayout>
小夥伴們可以用10086進行測試