2012-09-17 33 views
0

我有一個廣播接收器,在電話啓動時運行一個待定的意圖。掛起的意圖打開一個對話框來提醒用戶。同步對話框

我注意到,如果有幾個對話框的時間已經過去,它們都會立即啓動。這導致僅顯示第一個對話框。當你點擊'解除'按鈕時,它會解除所有的對話框。

有沒有辦法讓每個盒子一次顯示一張而不是像這樣卡紙?

我讀到了使用「同步」,但它似乎只用於網絡。你能告訴我什麼是解決這個問題的最好方法嗎?

我的代碼:

package com.google.android.gcm.demo.app.Alerts; 

import java.io.IOException; 

import com.google.android.gcm.demo.app.DemoActivity; 
import com.google.android.gcm.demo.app.PreferenceConnector; 
import com.google.android.gcm.demo.app.R; 
import com.google.android.gcm.demo.app.TabBarExample; 
import com.google.android.gcm.demo.app.sqllite.DatabaseSqlite; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.res.AssetFileDescriptor; 
import android.media.AudioManager; 
import android.media.MediaPlayer; 
import android.media.Ringtone; 
import android.media.RingtoneManager; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Vibrator; 

public class AlertDialogActivity extends Activity { 

    MediaPlayer player = new MediaPlayer(); 
    Bundle bundle; 
    Vibrator vibrate; 
    DatabaseSqlite entry = new DatabaseSqlite(AlertDialogActivity.this); 
    int vibrateState; 
    int soundState; 
    AlertDialog.Builder builder; 
    Intent intent; 
    int idInteger; 
    int alertDismissed = 1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     bundle = getIntent().getExtras(); 
     String name = bundle.getString("name"); 
     String id = bundle.getString("id"); 

     idInteger = Integer.parseInt(id); 

     intent = new Intent(AlertDialogActivity.this, TabBarExample.class); 
     sound(); 
     vibrate(); 

     entry.open(); 
     entry.updateAlertDismissedState(idInteger, alertDismissed); 
     String text = entry.getMinutesForOneShot(idInteger); 
     entry.close(); 

     String textForDisplay = "In " + text + " Minutes"; 

     builder = new AlertDialog.Builder(this); 
     builder.setMessage(textForDisplay) 
       .setTitle(name) 
       .setCancelable(false) 
       .setNegativeButton("Dismiss", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 

           soundState = PreferenceConnector.readInteger(
             AlertDialogActivity.this, 
             PreferenceConnector.SOUND_ON_OFF, 0); 
           if (soundState == 0) { 

            player.pause(); 
            player.release(); 

           } 

           vibrateState = PreferenceConnector.readInteger(
             AlertDialogActivity.this, 
             PreferenceConnector.VIBRATE_ON_OFF, 0); 

           if (vibrateState == 0) { 

            vibrate.cancel(); 
           } 

           startActivity(intent); 

          } 
         }); 

     AlertDialog alert = builder.create(); 
     alert.show(); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 

    } 

    private void sound() { 

     soundState = PreferenceConnector.readInteger(AlertDialogActivity.this, 
       PreferenceConnector.SOUND_ON_OFF, 0); 
     if (soundState == 0) { 

      // play sound 

      AssetFileDescriptor afd = this.getResources().openRawResourceFd(
        R.raw.alarm_clock); 
      try { 
       player.setDataSource(afd.getFileDescriptor(), 
         afd.getStartOffset(), afd.getLength()); 
       afd.close(); 
       player.setAudioStreamType(AudioManager.STREAM_ALARM); 
       player.setLooping(true); 
       player.prepare(); 
       player.start(); 

      } catch (IllegalArgumentException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IllegalStateException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } else { 
     } 

    } 

    private void vibrate() { 

     vibrateState = PreferenceConnector 
       .readInteger(AlertDialogActivity.this, 
         PreferenceConnector.VIBRATE_ON_OFF, 0); 

     if (vibrateState == 0) { 
      // Get instance of Vibrator from current Context 
      vibrate = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 

      // Start immediately 
      // Vibrate for 200 milliseconds 
      // Sleep for 500 milliseconds 
      long[] pattern = { 0, 200, 500 }; 

      // The "0" means to repeat the pattern starting at the beginning 
      // CUIDADO: If you start at the wrong index (e.g., 1) then your 
      // pattern will be off -- 
      // You will vibrate for your pause times and pause for your vibrate 
      // times ! 
      vibrate.vibrate(pattern, 0); 
     } else { 
     } 

    } 

} 

回答

0

爲了解決這個問題,我比較當前日期當警報發射了。如果警報通過了我,我把它列入一個列表。然後我用意圖將列表發送到另一個活動。在那個活動中,我用另一個for循環來一次處理一個對話框。