我有一个广播接收器,在手机启动时运行待定意图。待处理的意图打开一个对话框以提醒用户。
我注意到如果有几个对话框已经过了时间,那么它们都会立即启动。这导致仅显示第一个对话框。当您单击“关闭”按钮时,它会关闭所有对话框。
有没有办法让每个盒子一次显示一个,而不是像这样堵塞?
我读到了使用'synchronize',但它似乎只用于网络。你能告诉我解决这个问题的最佳方法是什么吗?
我的代码:
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 {
}
}
}