我可以在android 4.1.2中发送“短信接收意图”吗?

时间:2013-04-22 09:04:49

标签: android sms

此代码不适用于Android 4.1.2版,

private static 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) 
     {
     }
     pdu = bo.toByteArray();
     }  
     catch (IOException e) 
     {
     }
    Intent intent = new Intent();
    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.startService(intent);
}

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

当我致电context.startService(intent)时,我收到此异常

java.lang.SecurityException: Not allowed to start service Intent { act=android.provider.Telephony.SMS_RECEIVED cmp=com.android.mms/.transaction.SmsReceiverService (has extras) } without permission not exported from uid 10046**

在虚拟设备4.1.2上它完美运行但不在我的GS2设备中。

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:4)

在较新版本的Android中,这是受保护的广播,出于安全考虑,只能由系统应用程序发送。

让您的应用程序发送它的唯一方法是说服OEM将其作为ROM上的系统应用程序