2012-04-04 53 views
0

我的應用程序接收到SMS並更改爲活動以在我的應用程序中顯示一個警告對話框。 Toast運行良好,但不會改變活動。 onReceive()接收包含電子郵件的SMS,並根據該電子郵件ID,我的應用程序搜索關聯的聯繫人號碼並將其發回回覆消息。BroadcastReceiver類別的開始意圖

public void onReceive(Context context, Intent intent) 
{ 
    // Get SMS map from Intent 
    Bundle extras = intent.getExtras(); 

    String messages = ""; 

    if (extras != null) 
    { 
     // Get received SMS array 
     Object[] smsExtra = (Object[]) extras.get("pdus"); 

     // Get ContentResolver object for pushing encrypted SMS to incoming folder 
     //ContentResolver contentResolver = context.getContentResolver(); 

     for (int i = 0; i < smsExtra.length; ++i) 
     { 
      SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]); 

      String body = sms.getMessageBody().toString(); 
      String address = sms.getOriginatingAddress(); 

      messages += "SMS from " + address + " :\n";      
      messages += body + "\n"; 

      // Here you can add any your code to work with incoming SMS 
      // I added encrypting of all received SMS 


     } 

     // Display SMS message 
     Toast.makeText(context, messages, Toast.LENGTH_SHORT).show(); 
     Intent i=new Intent(context,AlertActivity.class); 
     // context.startActivity(i); 
     i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

    } 
+0

爲什麼你評論這個'context.startActivity(i);'line? – 2012-04-04 10:42:18

回答

2

您在啓動AlertActivity活動後添加addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)。使用這種方式:

Intent i=new Intent(context,AlertActivity.class); 

i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

context.startActivity(i); 
+0

非常感謝你的工作 – venkyMCA 2012-04-07 05:43:57