2013-04-22 51 views
0

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

这是我的短信接收处理类作为TOAST。这是不是我真的:

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

张贴您正在使用 – FoamyGuy 2013-04-22 02:28:57

+0

@FoamyGuy广播接收器,我贴我的。抱歉格式不正确。 – 2013-04-22 11:21:57

回答

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(); 
+0

我会尽力让你知道。谢谢 – 2013-04-22 15:07:49

+0

它仍然是一样的。短信将进入收件箱,但不在TOAST – 2013-04-22 15:18:39

+0

当呼叫处于振铃状态时,我想将收到的短信显示为吐司。 – 2013-04-22 15:35:45