2012-12-04 94 views
0

先生,我如何提示停止振鈴的警報,然後在其他類中啓動alertdialog?此代碼始終強制關閉。如果我刪除提示部分來停止時鐘,鬧鐘不會停止振鈴,並且alertdialog只顯示一次。如果它收到另一條消息,它將不會再次觸發alertdialog。請幫幫我。感謝您的幫助提前如何提示停止振鈴並啓動alertdialog的警報

for (SmsMessage msg : messages) { 
     if (msg.getMessageBody().contains("alert")) { 

      Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); 
      if(alert == null){ 
       // alert is null, using backup 
       alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
       if(alert == null){ 
        // alert backup is null, using 2nd backup 
        alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);    
       } 
      } 
      Ringtone r = RingtoneManager.getRingtone(context.getApplicationContext(), alert); 
      AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE); 
      int maxVolumeAlarm = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM); 

      audioManager.setStreamVolume(AudioManager.STREAM_ALARM, maxVolumeAlarm,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE); 

      r.play(); 

//prompt 
alertDialogBuilder = new AlertDialog.Builder(
      context); 
    alertDialogBuilder.setTitle("Alarm Received"); 

      alertDialogBuilder 
      .setMessage("Stop alarm") 
      .setCancelable(false) 
      .setPositiveButton("OK",new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog,int id) { 
        r.stop(); 
        Toast.makeText(getApplicationContext(), "alarm stopped", Toast.LENGTH_LONG).show(); 
       } 
       }); 

      // create alert dialog 
      AlertDialog alertDialog = alertDialogBuilder.create(); 

      // show it 
      alertDialog.show(); 
       Toast.makeText(context.getApplicationContext(), "alarm started", Toast.LENGTH_LONG).show(); 

//open alertdialog in other activity   
Intent openInterface = new Intent("proj.receiver.RECEIVERINTERFACE"); 
      openInterface.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      context.startActivity(openInterface); 

     }//end if 
    }//end for 

回答

0

我不明白你在所有的,但我明白:

  1. 的「r.stop()」對話框裏面是什麼使報警停止。所以如果你不想讓報警停止,那就從那裏移動它。

  2. 只有當'message'是'alert'(if)時,對話框纔會打開併發出警報,所以如果您希望它在不同的消息中發生,您必須創建自己的'if-elseif'。

  3. 如果你想在另一個不同的信息中停止報警(所以第1點和第2點),你必須在「if」句子之外移動「Ringtone r = ....」和/或取決於你希望在'for'聲明之前做。

-

要做到這一點在其他活動中,創建一個靜態類,鈴聲變量,當您創建鈴聲VAR傳遞到靜態類儲存。然後當新的Activity打開時,從StaticClass獲取var來停止振鈴。 希望它有幫助。

+0

我的代碼沒有運行,如果我離開r.stop()那裏,它只是強制關閉。 –

+0

你在'r.stop()'後面試過了'dialog.cancel()'和/或'dialog.dismiss()'嗎? – Jordi