每次在Android(Kit Kat)中收到数据短信失败

时间:2014-06-28 11:06:28

标签: android android-intent sendmessage

当我将手机 LG G2 的数据讯息与我的应用程序发送到特定端口时,应用程序崩溃了。如果我在AVD中测试应用程序并单击发送按钮一段时间,它会显示(无响应),然后显示发件人的确定消息,但不会收到消息。 我的发送代码:

private void sendSMS()
{
    PendingIntent sent = this.createPendingResult(SENT, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
    String messageText = _message.getText().toString();

    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendDataMessage(_phoneNumber.getText().toString(), null, SMS_PORT, messageText.getBytes(), sent, null);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    String toast = null;
    switch (requestCode)
    {
      case SENT :
        switch (resultCode)
        {
          case RESULT_OK :
            toast = "OK";
            break;
          case SmsManager.RESULT_ERROR_GENERIC_FAILURE :
            toast = "Generic Failure";
            break;
          case SmsManager.RESULT_ERROR_RADIO_OFF :
            toast = "Radio Off";
            break;
          case SmsManager.RESULT_ERROR_NULL_PDU :
            toast = "Null Pdu";
            break;
        }
        break;
    }

    if (toast != null)
    {
      Toast.makeText(this, toast, Toast.LENGTH_LONG).show();
    }
}

我收到的代码:

public class Receiver extends BroadcastReceiver {
private int id;

    @Override
    public void onReceive(Context context, Intent intent)
    {
       Bundle bundle = intent.getExtras();

String recMsgString = "";
String fromAddress = "";

SmsMessage recMsg = null;
byte[] data = null;

Random r = new Random();
id = r.nextInt(r.nextInt());
if (bundle != null)
{
  //---retrieve the SMS message received---
  Object[] pdus = (Object[]) bundle.get("pdus");
  for (int i = 0; i < pdus.length; i++)
  {
    recMsg = SmsMessage.createFromPdu((byte[]) pdus[i]);

    try
    {
      data = recMsg.getUserData();
    }
    catch (Exception e)
    {

    }
    if (data != null)
    {
      for (int index = 0; index < data.length; ++index)
      {
        recMsgString += Character.toString((char) data[index]);
      }
    }

    fromAddress = recMsg.getOriginatingAddress();
    notification(context, "From: " + fromAddress, recMsgString);
  }
 }
}

private void notification(Context context, String title, String content) {
    NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(context)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle(title)
        .setContentText(content);

Intent resultIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(
            0,
            PendingIntent.FLAG_UPDATE_CURRENT
        );
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(id, mBuilder.build());
}
}

在清单文件中,我添加了权限: SEND_SMS RECEIVE_SMS READ_SMS 和收件人标记:

<receiver android:name=".Receiver" >
        <intent-filter>
            <action android:name="android.intent.action.DATA_SMS_RECEIVED" />

            <data android:scheme="sms" />
            <data android:port="8998" />
        </intent-filter>
    </receiver>

如果我更改代码以发送短信,一切正常,但我需要从我的应用程序发送的邮件对其他人不可见。

0 个答案:

没有答案