我的应用程序出现了下一个问题。我为闹钟创建了通知,并在设置闹钟时显示通知。在应用程序中我有2个按钮,保存和取消。这些按钮处于相同位置,取消按钮不可见,单击保存后,保存按钮隐藏,取消按钮可见。但我有一个问题,因为当我从通知按钮启动我的应用程序时,默认配置。我的意思是即使设置了警报,取消按钮也是不可见的。我试图使用来自另一个线程的解决方案,但那不起作用(清单中的单个实例或意图的FLAG_ACTIVITY_SINGLE_TOP)。你能解释一下我做错了什么以及如何修复它?谢谢。
Alarm.java
private void notification(){
Intent intentNotification=new Intent(this.getApplicationContext(),Alarm.class);
intentNotification.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); // Not working
PendingIntent pendingIntentNotification=PendingIntent.getActivity(this,0,intentNotification,0);
Notification notification=new Notification.Builder(this)
.setContentTitle("Alarm")
.setContentText("Next alarm: "+hourString+":"+minuteString)
.setContentIntent(pendingIntentNotification)
.setAutoCancel(false)
.setSmallIcon(R.mipmap.ic_launcher)
.build();
notificationManager.notify(0,notification);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm);
vibrationsSwitch = findViewById(R.id.vibrationsSwitch);
timePicker = findViewById(R.id.timePicker);
noteButton = findViewById(R.id.noteButton);
saveButton = findViewById(R.id.saveButton);
cancelButton = findViewById(R.id.cancelButton);
wifiCheckBox = findViewById(R.id.wifiCheckBox);
soundCheckBox = findViewById(R.id.soundCheckBox);
bluetoothCheckBox = findViewById(R.id.bluetoothCheckBox);
wiFi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
setmVibrator((Vibrator) getSystemService(Context.VIBRATOR_SERVICE));
intent = new Intent(this, AlarmReceiver.class);
timePicker.setIs24HourView(true);
setCancelButton((Button) findViewById(R.id.cancelButton));
setSaveButton((Button) findViewById(R.id.saveButton));
initControls();
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
if (Build.VERSION.SDK_INT >= 23) {
calendar.set(Calendar.HOUR_OF_DAY, timePicker.getHour());
calendar.set(Calendar.MINUTE, timePicker.getMinute());
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
hour = timePicker.getHour();
minute = timePicker.getMinute();
} else {
calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
hour = timePicker.getCurrentHour();
minute = timePicker.getCurrentMinute();
}
hourString = String.valueOf(hour);
minuteString = String.valueOf(minute);
if (hour == 0)
hourString = "0" + hourString;
if (minute < 10)
minuteString = "0" + minuteString;
intent.putExtra("extra", "on");
if (calendar.before(Calendar.getInstance()))
calendar.add(Calendar.DATE, 1);
setAlarm(calendar.getTimeInMillis());
notification();
saveButton.setVisibility(View.INVISIBLE);
cancelButton.setVisibility(View.VISIBLE);
}
});
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cancelButton();
cancelButton.setVisibility(View.INVISIBLE);
saveButton.setVisibility(View.VISIBLE);
}
});
noteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog();
}
});
}
private void setAlarm(long timeInMillis) {
pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
this.alarmManager.setExactAndAllowWhileIdle(
AlarmManager.RTC_WAKEUP,
timeInMillis,
pendingIntent
);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
this.alarmManager.setExact(
AlarmManager.RTC_WAKEUP,
timeInMillis,
pendingIntent
);
} else {
this.alarmManager.set(
AlarmManager.RTC_WAKEUP,
timeInMillis,
pendingIntent
);
}
}
RingtoneService.java
public class RingtoneService extends Service {
private MediaPlayer mediaPlayer;
private AudioManager audioManager;
private static int currentVolume;
private static int maxVolume;
private static int ringerMode;
boolean isRunning;
int startId;
@Override
public void onDestroy() {
this.isRunning = false;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public int onStartCommand(Intent intent, int flags, int startId) {
String state = intent.getExtras().getString("extra");
assert state != null;
switch (state) {
case "on":
startId = 1;
break;
case "off":
startId = 0;
break;
default:
startId = 0;
break;
}
Alarm alarm = new Alarm();
if (!this.isRunning && startId == 1) {
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
setCurrentVolume(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
setMaxVolume(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
setRingerMode(audioManager.getRingerMode());
mediaPlayer = MediaPlayer.create(this, Settings.System.DEFAULT_RINGTONE_URI);
mediaPlayer.setLooping(true);
// audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, getMaxVolume(), getMaxVolume());
// audioManager.setRingerMode(getRingerMode());
alarm.setVibrationsSwitch();
mediaPlayer.start();
notificationFiringAlarm(this);
this.isRunning = true;
this.startId = 0;
} else if (this.isRunning && startId == 0) {
mediaPlayer.stop();
alarm.turnOffVibrations();
mediaPlayer.reset();
this.isRunning = false;
this.startId = 0;
} else if (!this.isRunning && startId == 0) {
this.isRunning = false;
this.startId = 0;
} else if (this.isRunning && startId == 1) {
this.isRunning = true;
this.startId = 1;
}
return START_NOT_STICKY;
}
}