如何创建模拟传入短信的意图

时间:2017-02-02 12:31:43

标签: android android-intent broadcastreceiver android-sms

现在我可以通过下一个代码收到短信:

override fun onReceive(c: Context, i: Intent) {
        val bundle = intent.extras
        if (bundle != null && intent.action == SMS_RECEIVED) {
            var currentMessage: SmsMessage
            var msgs: Array<SmsMessage>? = null
            var pdusObj: Array<Any>? = null
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                msgs = Telephony.Sms.Intents.getMessagesFromIntent(intent)
                currentMessage = msgs!![0]
            } else {
                pdusObj = bundle.get("pdus") as Array<Any>
                currentMessage = SmsMessage.createFromPdu(pdusObj[0] as ByteArray)
            }
            mPhoneNumb = currentMessage.displayOriginatingAddress
}

我想知道是否有任何方式可以做出某种逆转: 为传入的短信模拟创建一个意图(带有我的电话号码和短信)。

有没有办法做到这一点?

更新

感谢MikeM找到了这个解决方案:Create PDU for Android that works with SmsMessage.createFromPdu() (GSM 3gpp)

但是我仍然无法向我的广播接收器发送假短信,这次我因下一个错误而崩溃:

SecurityException: Permission Denial: not allowed to send broadcast android.provider.Telephony.SMS_RECEIVED from pid=14860, uid=10621

但我已经获得了许可!这是我的清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.test.test">

    <uses-permission android:name="android.permission.READ_SMS"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-permission android:name="android.permission.VIBRATE"/>

<application
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <receiver android:name=".receiver.SmsReceiver">
            <intent-filter android:priority="1000">
                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
            </intent-filter>
        </receiver>
</application>
</manifest>

这是我的班级必须创建并向我的接收器发送假短信:

public class SendTestSms {

    public void createFakeSms(Context context, String sender,
                                      String body) {
        byte[] pdu = null;
        byte[] scBytes = PhoneNumberUtils
                .networkPortionToCalledPartyBCD("0000000000");
        byte[] senderBytes = PhoneNumberUtils
                .networkPortionToCalledPartyBCD(sender);
        int lsmcs = scBytes.length;
        byte[] dateBytes = new byte[7];
        Calendar calendar = new GregorianCalendar();
        dateBytes[0] = reverseByte((byte) (calendar.get(Calendar.YEAR)));
        dateBytes[1] = reverseByte((byte) (calendar.get(Calendar.MONTH) + 1));
        dateBytes[2] = reverseByte((byte) (calendar.get(Calendar.DAY_OF_MONTH)));
        dateBytes[3] = reverseByte((byte) (calendar.get(Calendar.HOUR_OF_DAY)));
        dateBytes[4] = reverseByte((byte) (calendar.get(Calendar.MINUTE)));
        dateBytes[5] = reverseByte((byte) (calendar.get(Calendar.SECOND)));
        dateBytes[6] = reverseByte((byte) ((calendar.get(Calendar.ZONE_OFFSET) + calendar
                .get(Calendar.DST_OFFSET)) / (60 * 1000 * 15)));
        try {
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            bo.write(lsmcs);
            bo.write(scBytes);
            bo.write(0x04);
            bo.write((byte) sender.length());
            bo.write(senderBytes);
            bo.write(0x00);
            bo.write(0x00); // encoding: 0 for default 7bit
            bo.write(dateBytes);
            try {
                String sReflectedClassName = "com.android.internal.telephony.GsmAlphabet";
                Class cReflectedNFCExtras = Class.forName(sReflectedClassName);
                Method stringToGsm7BitPacked = cReflectedNFCExtras.getMethod(
                        "stringToGsm7BitPacked", new Class[] { String.class });
                stringToGsm7BitPacked.setAccessible(true);
                byte[] bodybytes = (byte[]) stringToGsm7BitPacked.invoke(null,
                        body);
                bo.write(bodybytes);
            } catch (Exception e) {
                e.printStackTrace();
            }

            pdu = bo.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Intent intent = new Intent(context, SmsReceiver.class);
        intent.setClassName("com.android.mms",
                "com.android.mms.transaction.SmsReceiverService");
        intent.setAction("android.provider.Telephony.SMS_RECEIVED");
        intent.putExtra("pdus", new Object[] { pdu });
        intent.putExtra("format", "3gpp");
        context.sendBroadcast(intent);
    }

    private static byte reverseByte(byte b) {
        return (byte) ((b & 0xF0) >> 4 | (b & 0x0F) << 4);
    }
}

为什么我的许可对于发送广播无关紧要?

0 个答案:

没有答案