2016-11-14 57 views
0

我爲Android設置了一個警報,它還傳遞用於對話框的字符串值。我設置的第一個警報工作,並使用我傳遞的字符串值。但是,對於我設置的以下警報,它仍然使用傳遞的原始字符串值。Android - 在Alert對話框中爲字符串傳遞字符串值

我真的不知道什麼是錯的。這裏是我的代碼:

//alert method from the MainActivity 
    private void setAlert(int alertType, Calendar alertCalendar) { 
     //identify which alert 
     switch (alertType) { 
      case 1: 
       passFunction = "COURSE START"; 
       passValue = thisTermNum + " is starting on " + oldTermStart; 
       break; 
      case 2: 
       passFunction = "COURSE END"; 
       passValue = thisTermNum + " is ending on " + oldTermEnd; 
       break; 
     } 

     //actual alert 
     AlarmManager alertManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
     Intent alertIntent = new Intent(MainActivity.this, AlertReceiver.class); 
     Bundle alertBundle = new Bundle(); 
     alertBundle.putString(PASS_ALERT_FUNCTION, passFunction); 
     alertBundle.putString(PASS_ALERT_VALUE, passValue); 
     alertIntent.putExtras(alertBundle); 
     PendingIntent pendingAlert = PendingIntent.getBroadcast(MainActivity.this, 0, alertIntent, 0); 
     alertManager.set(AlarmManager.RTC_WAKEUP, alertCalendar.getTimeInMillis(), pendingAlert); 

     Toast.makeText(this, passFunction + " HAS BEEN SET", Toast.LENGTH_SHORT).show(); 
    } 

這是接收機類:

public class AlertReceiver extends WakefulBroadcastReceiver{ 

     //for passed values 
     String PASS_ALERT_FUNCTION = "PassAlertFunction"; 
     String PASS_ALERT_VALUE = "PassAlertValue"; 
     String receivedFunction; 
     String receivedValue; 


     @Override 
     public void onReceive(Context context, Intent intent) { 

      //receive passed values 
      Bundle getAlertExtra = intent.getExtras(); 
      receivedFunction = getAlertExtra.getString(PASS_ALERT_FUNCTION); 
      receivedValue = getAlertExtra.getString(PASS_ALERT_VALUE); 

      //for alert sound 
      Uri alertSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); 
      Ringtone alertTone = RingtoneManager.getRingtone(context, alertSound); 
      alertTone.play(); 

      //to pass value to AlertActivity for the alert dialog box 
      Intent passAlertValues = new Intent(context, AlertActivity.class); 
      Bundle alertBundle = new Bundle(); 
      alertBundle.putString(PASS_ALERT_FUNCTION, receivedFunction); 
      alertBundle.putString(PASS_ALERT_VALUE, receivedValue); 
      passAlertValues.putExtras(alertBundle); 
      passAlertValues.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      context.startActivity(passAlertValues); 
     } 
    } 

這是AlertActivity類,其中會出現對話框與傳遞的值:

public class AlertActivity extends AppCompatActivity { 

     //for passed values 
     String PASS_ALERT_FUNCTION = "PassAlertFunction"; 
     String PASS_ALERT_VALUE = "PassAlertValue"; 
     String receivedFunction; 
     String receivedValue; 

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

      //receive alert values 
      Intent alertValues = getIntent(); 
      Bundle alertBundle = alertValues.getExtras(); 
      receivedFunction = alertBundle.getString(PASS_ALERT_FUNCTION); 
      receivedValue = alertBundle.getString(PASS_ALERT_VALUE); 

      //dialog box for the alert 
      AlertDialog.Builder alertBuidler = new AlertDialog.Builder(this); 
      alertBuidler.setTitle(receivedFunction) 
        .setMessage(receivedValue) 
        .setCancelable(false) 
        .setPositiveButton("OK", new DialogInterface.OnClickListener() { 

         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          Intent intent = new Intent(AlertActivity.this, MainActivity.class); 
          startActivity(intent); 
          finish(); 
         } 
        }); 
      AlertDialog alertDialog = alertBuidler.create(); 
      alertDialog.show(); 
     } 
    } 

回答

0

對於以下行代碼:

PendingIntent pendingAlert = PendingIntent.getBroadcast(MainActivity.this, 0, alertIntent, 0); 

對第二個參數使用一些其他的int值,否則它將返回相同的未決意圖。該醫生說

的PendingIntent返回一個現有的或新的PendingIntent匹配 給出的參數。僅當提供FLAG_NO_CREATE爲 時纔可以返回空值。

請參閱this瞭解更多。

+0

IT WORKED !!!非常感謝! – afrocode

相關問題