android 中發送短信

在虛擬機上玩了幾天iphone,環境實在搭不起來,只能丟盔棄甲了,好歹object-c算是入了門,欣賞了另一種oop的實現方法,算是過了一把癮。android

人一閒下來實在是難受,看看android算了。iphone

android中發送短信很簡單,ide

首先要在Mainfest.xml中加入所須要的權限:oop

<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>測試

爲了後面測試方便,乾脆把SMS的全部權限加上。this

方法1:加密

public class testSms extends Activity {xml

...ip

    private void send1(String phone, String message){get

        PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, testSms.class), 0);

        SmsManager sms = SmsManager.getDefault();

        sms.sendTextMessage(phone, null, message, pi, null);

    }

}

方法2:

若是想知道短信發送後的狀態,須要兩個Receiver來實現

private void send2(String number, String message){
    String SENT = "sms_sent";
    String DELIVERED = "sms_delivered";
    PendingIntent sentPI = PendingIntent.getActivity(this, 0, new Intent(SENT), 0);
    PendingIntent deliveredPI = PendingIntent.getActivity(this, 0, new Intent(DELIVERED), 0);
    registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context context, Intent intent) {
                switch(getResultCode())
                {
                    case Activity.RESULT_OK:
                        Log.i("====>", "Activity.RESULT_OK");
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Log.i("====>", "RESULT_ERROR_GENERIC_FAILURE");
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Log.i("====>", "RESULT_ERROR_NO_SERVICE");
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Log.i("====>", "RESULT_ERROR_NULL_PDU");
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Log.i("====>", "RESULT_ERROR_RADIO_OFF");
                        break;
                }
            }
    }, new IntentFilter(SENT));
    registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent){
            switch(getResultCode())
            {
                case Activity.RESULT_OK:
                    Log.i("====>", "RESULT_OK");
                    break;
                case Activity.RESULT_CANCELED:
                    Log.i("=====>", "RESULT_CANCELED");
                    break;
            }
        }
    }, new IntentFilter(DELIVERED));
        SmsManager smsm = SmsManager.getDefault();
        smsm.sendTextMessage(number, null, message, sentPI, deliveredPI);
}

在模擬器上是看不到Receiver中的log信息的,據網上說在手機上是能夠實現的,只惋惜手中無機,也只能紙上談兵了。

方法3:

上面都是發送文本文件,若是想發送一些非文本,好比加密數據,能夠用如下方法:

private void send2(String number, String message){
    SmsManager smsm = SmsManager.getDefault();
    short port = 1000;
    PendingIntent pi = PendingIntent.getBroadcast(test.this, 0, new Intent(), 0);
    smsm.sendDataMessage(number, null, port, message.getBytes(), pi, null);
}

方法4:

調用系統的短信界面,這個方法須要用戶本身輸入接收方的電話號碼

private void send(String message){
    Intent sendIntent = new Intent(Intent.ACTION_VIEW);
    sendIntent.putExtra("sms_body", message);
    sendIntent.setType("vnd.android-dir/mms-sms");
}

這個方法自動設置接收方的號碼

private void send1(String number, String message){
    Uri uri = Uri.parse("smsto:" + number);
    Intent sendIntent = new Intent(Intent.ACTION_VIEW, uri);
    sendIntent.putExtra("sms_body", message);
    startActivity(sendIntent);
}

短信的接受,須要實現BroadcastReceiver類,監聽系統消息

首先在Mainfest.xml中加入申明,SmsReceiver爲實現類

<receiver android:name=".SmsReceiver">

<intent-filter>
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>

</receiver>

若是是方法3,使用sendDataMessage時須要加入

<receiver android:name=".SmsReceiver">

<intent-filter>
    <action android:name="android.intent.action.DATA_SMS_RECEIVED" />
    <data android:scheme="sms" />
    <data android:host="localhost" />
    <data android:port="1000" />
</intent-filter>

</receiver>

將其中的dat數據註釋掉,模擬器上也能夠接收到短信; port跟sendDataMessage中的端口值不一致時,也沒有問題,太神奇了

public class SmsReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;
        String phone;
        String message;
        if(bundle != null){
            Object[] pdus = (Object[])bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];
            for(int i = 0; i < msgs.length; i++){
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                phone = msgs[i].getOriginatingAddress();
                message = msgs[i].getMessageBody();
            }
        }
    }
}

若是是sendDataMessage發送:

public class SmsReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;
             String phone;
             String message;
        if(bundle != null){
            Object[] pdus = (Object[])bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];
            for(int i = 0; i < msgs.length; i++){
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                phone = msgs[i].getOriginatingAddress();
                byte data[] = SmsMessage.createFromPdu((byte[])pdus[i]).getUserData();
                message = new String(data);
            }
        }
    }
}

不同的地方是message的獲取時使用getUserData()方法。

相關文章
相關標籤/搜索