扣丁学堂——收发短信


单击事件中发送:

 public void mmsClick(View v){
        SmsManager sm = SmsManager.getDefault();

        String message = "一条短线";

       ArrayList<String> list =  sm.divideMessage(message);

        for (int i=0;i<list.size();i++){
            sm.sendTextMessage("5556",null,list.get(i),null,null);
        }

    }

扣丁学堂——收发短信_第1张图片

public class MyReceiverR extends BroadcastReceiver {
    public MyReceiverR() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        Bundle bundle = intent.getExtras();

        if(bundle !=null){
            Object[] objects = (Object[]) bundle.get("pdus");

            SmsMessage[] smsMessages = new SmsMessage[objects.length];

            for (int i=0;i<objects.length;i++){
                smsMessages[i] = SmsMessage.createFromPdu((byte[]) objects[i]);

                //发送方的号码
              String number =   smsMessages[i].getDisplayOriginatingAddress();
                //获取短线的内容
               String cont =  smsMessages[i].getDisplayMessageBody();


                Toast.makeText(context,number +":"+cont,Toast.LENGTH_SHORT).show();
            }
        }

    }
}

//清单配置
        <receiver
            android:name=".MyReceiverR"
            android:enabled="true"
            android:exported="true" >

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

            </intent-filter>

        </receiver>


你可能感兴趣的:(扣丁学堂——收发短信)