按钮在onclick处更改,这就是为什么有一个标志。但是警报管理器并没有停止意图,它甚至在onclick上也会一直运行以取消alarmManager
if (flag) {
flag = false;
imageButton.setImageResource(R.drawable.mybuttonbc);
Intent intent = new Intent(HomeActivity.this, IntentServices.class);
intent.setAction("upload");
intent.putExtra("lat", lats);
intent.putExtra("long", longs);
final PendingIntent pintent = PendingIntent.getService(HomeActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 60000, pintent);
Snackbar.make(getWindow().getDecorView().getRootView(), "recording started!", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
} else {
flag = true;
imagePanicButton.setImageResource(R.drawable.mybuttona);
Intent intent = new Intent(HomeActivity.this, IntentServices.class);
final PendingIntent pIntent = PendingIntent.getService(HomeActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pIntent);
Snackbar.make(getWindow().getDecorView().getRootView(), "recording stopped!", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
答案 0 :(得分:0)
您缺少用于最初创建警报的Intent中的操作。
只需将此行添加到取消警报的代码中:
intent.setAction("upload");
所以它看起来像这样:
if (flag) {
flag = false;
imageButton.setImageResource(R.drawable.mybuttonbc);
Intent intent = new Intent(HomeActivity.this, IntentServices.class);
intent.setAction("upload");
intent.putExtra("lat", lats);
intent.putExtra("long", longs);
final PendingIntent pintent = PendingIntent.getService(HomeActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 60000, pintent);
Snackbar.make(getWindow().getDecorView().getRootView(), "recording started!", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
} else {
flag = true;
imagePanicButton.setImageResource(R.drawable.mybuttona);
Intent intent = new Intent(HomeActivity.this, IntentServices.class);
intent.setAction("upload"); // <-Added
final PendingIntent pIntent = PendingIntent.getService(HomeActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pIntent);
Snackbar.make(getWindow().getDecorView().getRootView(), "recording stopped!", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}