0

我需要你的帮助,就像你以前一样。我的应用程序的一部分是关于在 android 中发送和接收 SMS。我做了很多研究,特别是过去在这个论坛上发帖。但我没有得到我正在寻找的答案。我能够在接收安卓手机上发送和接收短信。重要的是我想处理收到的短信以将其显示为 TOAST。它只会发送到带有通知但没有 TOAST 的收件箱。同时,我在我的 broadcastReceiver 类中接收我的拨出电话号码,我想知道将 SMS 处理为 TOAST 的规范。请帮我!

这是我作为 TOAST 进行 SMS 处理的接收器类。它真的不是我的:

import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.telephony.SmsMessage;

    public class Receiver extends BroadcastReceiver
    {

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

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

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

        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();
          }
        }

        Intent result = new Intent(context, ReceiveActivity.class);
        result.putExtra(SMSReceive.MESSAGE, "Received SMS from " + fromAddress + ": " + recMsgString);
        result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(result);
      }
    }




    ReceiverActivity.java



           public class ReceiverActivity extends Activity
            {

              @Override
              protected void onCreate(Bundle savedInstanceState)
              {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                Intent intent = getIntent();

                String message = intent.getStringExtra(MESSAGE);
                Log.i("This is the message", message);

                if (message != null)
                {
                    Toast.makeText(getApplicationContext(),message, Toast.LENGTH_LONG).show();
                }
              }
            }
4

1 回答 1

0

试试这样:

    /*
    Intent result = new Intent(context, ReceiveActivity.class);
    result.putExtra(SMSReceive.MESSAGE, "Received SMS from " + fromAddress + ": " + recMsgString);
    result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(result);
    */
    Toast.makeText(context, recMsgString, Toast.LENGTH_SHORT).show();
于 2013-04-22T13:21:41.493 回答