源碼下載地址:https://download.csdn.net/download/kwunyamshan/11259876
#接收短信
###效果圖
1.接收廣播
/**
* @author idulc
*/
public class ReceiverSms extends BroadcastReceiver {
/**
* 以BroadcastReceiver接收SMS短信
*/
public static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
@Override
public void onReceive(Context context, Intent intent) {
if (ACTION.equals(intent.getAction())) {
Intent i = new Intent(context, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
SmsMessage[] msgs = getMessageFromIntent(intent);
StringBuilder sBuilder = new StringBuilder();
if (msgs != null && msgs.length > 0) {
for (SmsMessage msg : msgs) {
sBuilder.append("接收到了短信:\n發件人是:");
sBuilder.append(msg.getDisplayOriginatingAddress());
sBuilder.append("\n------短信內容-------\n");
sBuilder.append(msg.getDisplayMessageBody());
i.putExtra("sms_address", msg.getDisplayOriginatingAddress());
i.putExtra("sms_body", msg.getDisplayMessageBody());
}
}
Toast.makeText(context, sBuilder.toString(), Toast.LENGTH_SHORT).show();
context.startActivity(i);
}
}
public static SmsMessage[] getMessageFromIntent(Intent intent) {
SmsMessage retmeMessage[] = null;
Bundle bundle = intent.getExtras();
Object pdus[] = (Object[]) bundle.get("pdus");
retmeMessage = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
byte[] bytedata = (byte[]) pdus[i];
retmeMessage[i] = SmsMessage.createFromPdu(bytedata);
}
return retmeMessage;
}
}
2.清單文件
<!--接收短信權限-->
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<!--接收短信的廣播-->
<receiver android:name="receiver.ReceiverSms">
<intent-filter >
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
3.頁面展示
/**
* @author idulc
*/
public class MainActivity extends Activity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView1);
Intent intent = getIntent();
if (intent != null) {
String address = intent.getStringExtra("sms_address");
if (address != null) {
textView.append("發件人:\n" + address);
String bodyString = intent.getStringExtra("sms_body");
if (bodyString != null) {
textView.append("\n短信內容:\n" + bodyString);
}
}
}
}
}
4.layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@mipmap/bg"
android:padding="15dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:background="@drawable/item_bg"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:paddingLeft="30dip"
android:paddingTop="20dip"
android:paddingBottom="20dip"
android:paddingRight="20dip"
android:hint="短信內容:"
android:textSize="20sp"
android:textColor="#000" />
</LinearLayout>
</LinearLayout>
#發送短信兩種方式
##效果圖
- ui:
/**
* @author idulc
*/
public class SendSmsActivity extends Activity {
private static final String TAG = "SendSmsActivity";
//your phone
private String phone = "5554";
private String message = "Good things come to those who smile. Have you smiled today? Keep smiling. --好事情總是發生在那些微笑着的人身上。你今天微笑了麼?保持微笑哦";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sendsms);
}
/**
* 點擊直接發送短信
*
* @param view
*/
public void sendSmsAuto(View view) {
sendSms1(phone, message);
}
/**
* 點擊調用系統短信
*
* @param view
*/
public void sendSmsNoAuto(View view) {
sendSms2(phone, message);
}
/**
* 直接調用短信接口發短信
*
* @param phoneNumber
* @param message
*/
public void sendSms1(String phoneNumber, String message) {
if (phoneNumber != null) {
//獲取短信管理器
SmsManager smsManager = SmsManager.getDefault();
//拆分短信內容(手機短信長度限制)
List<String> divideContents = smsManager.divideMessage(message);
Log.i(TAG, divideContents.size() + "");
for (String text : divideContents) {
//destinationAddress:目標電話號碼
//scAddress:短信中心號碼,測試可以不填
//text: 短信內容
//sentIntent:發送 -->中國移動 --> 中國移動發送失敗 --> 返回發送成功或失敗信號 --> 後續處理 即,這個意圖包裝了短信發送狀態的信息
//處理返回的發送狀態
String SENT_SMS_ACTION = "SENT_SMS_ACTION";
Intent sentIntent = new Intent(SENT_SMS_ACTION);
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, sentIntent,
0);
//deliveryIntent: 發送 -->中國移動 --> 中國移動發送成功 --> 返回對方是否收到這個信息 --> 後續處理 即:這個意圖包裝了短信是否被對方收到的狀態信息(供應商已經發送成功,但是對方沒有收到)。
String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";
Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);
PendingIntent deliverPI = PendingIntent.getBroadcast(this, 0,
deliverIntent, 0);
/* 如果不需要廣播監聽成功發送的事件 sentPI 和 deliverPI 可傳null ,可以註釋掉76行-86行 */
smsManager.sendTextMessage(phoneNumber, null, text, sentPI, deliverPI);
}
} else {
Toast.makeText(this, "聯繫人號碼不能爲空", Toast.LENGTH_SHORT).show();
}
}
/**
* 調起系統發短信功能
*
* @param phoneNumber
* @param message
*/
public void sendSms2(String phoneNumber, String message) {
if (PhoneNumberUtils.isGlobalPhoneNumber(phoneNumber)) {
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + phoneNumber));
intent.putExtra("sms_body", message);
startActivity(intent);
}
}
}
2.layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:padding="20dip"
android:background="@mipmap/bg"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一種方式"
android:onClick="sendSmsAuto"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二種方式"
android:onClick="sendSmsNoAuto"
/>
</LinearLayout>
3.註冊廣播
/**
* @author idulc
*/
public class SendSms extends BroadcastReceiver {
private static final String TAG = "SendSms";
@Override
public void onReceive(Context context, Intent intent) {
try {
/* android.content.BroadcastReceiver.getResultCode()方法 */
switch (getResultCode()) {
/* 發送短信成功 */
case Activity.RESULT_OK:
Log.d(TAG, "發送短信成功");
break;
/* 表示普通錯誤 */
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
/*表示無線廣播被明確地關閉*/
case SmsManager.RESULT_ERROR_RADIO_OFF:
/*表示沒有提供pdu*/
case SmsManager.RESULT_ERROR_NULL_PDU:
default:
Log.d(TAG, "發送短信失敗");
break;
}
} catch (Exception e) {
e.getStackTrace();
}
}
}
4.清單文件
<!--發短信權限-->
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<!--發送短信的廣播-->
<receiver android:name=".receiver.SendSms">
<intent-filter >
<action android:name="SENT_SMS_ACTION"/>
<action android:name="DELIVERED_SMS_ACTION"/>
</intent-filter>
</receiver>
包目錄結構: