我使用SharePreference设置组警报。总的来说,每件事都很好,但我想用btn_cancel取消闹钟,但它没有。(虽然我使用StopService,但没有任何事情发生!)。请帮帮我,我该怎么办?
public class MyActivity extends Activity implements View.OnClickListener {
private static final String SHARED_PREFERENCES_NAME = "ALARM_NOTIFICATION";
public static final String REQUEST_CODE = "REQUEST_CODE";
public static final String TIME = "TIME";
public static final String ALARM_1_NAME = "ALARM_1";
public static final String ALARM_NAME = "ALARM_NAME";
public static TextView text1;
private Button set1;
public static long timer1 = 0;
private int requestCode = 0;
private Intent intent1 = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
set1 = (Button) findViewById(R.id.btn_set1);
alarmLinear = (LinearLayout) findViewById(R.id.alarmLayout);
text1 = (TextView) findViewById(R.id.add1);
set1.setOnClickListener(this);
text1.setOnClickListener(this);
findViewById(R.id.btn_cancle1).setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.add1: {
set1.setVisibility(View.VISIBLE);
if (timer1 == 0) {
timer1 = getCurrentTime();
} // get time from system
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timer1); //set time in timer parametr
TimePicker timePicker = (TimePicker) findViewById(R.id.tp_1);
timePicker.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY));
timePicker.setCurrentMinute(calendar.get(Calendar.MINUTE));
break;
}
case R.id.btn_set1: {
TimePicker timePicker = (TimePicker) findViewById(R.id.tp_1);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
calendar.set(Calendar.SECOND, 0);
timer1 = calendar.getTimeInMillis();
String stringTime = convertTimeToString(timer1);
text1.setText(stringTime);
if (intent1 == null) {
intent1 = new Intent(this, alarmService.class);
intent1.putExtra(ALARM_NAME , ALARM_1_NAME);
}
requestCode++;
intent1.putExtra(REQUEST_CODE, requestCode);
intent1.putExtra(TIME, timer1);
startService(intent1);
break;
}
case R.id.btn_cancle1:
cancelTimers();
break;
}
}
private long getCurrentTime() {
Calendar calendar = GregorianCalendar.getInstance();
return calendar.getTimeInMillis();
}
private void cancelTimers() {
if (intent1 != null) {
timer1 = 0;
stopService(intent1);
text1.setText("+");
}
}
这个AlarmService()类:
public class alarmService extends Service {
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent == null) {
stopSelf();
} else {
String alarmName = intent.getExtras().getString(MyActivity.ALARM_NAME);
Intent alarm = new Intent(this, alarm.class);
alarm.putExtra(MyActivity.ALARM_NAME,alarmName);
int requestCode = intent.getExtras().getInt(MyActivity.REQUEST_CODE);
long time = intent.getExtras().getLong(MyActivity.TIME);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(this, requestCode, alarm,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
long currentRTC = System.currentTimeMillis();
if (currentRTC <= time) {
alarmManager.set(AlarmManager.RTC, time, pendingIntent1);
}
}
return START_STICKY;
}
}
感谢。
答案 0 :(得分:1)
这里我们通过调用警报服务取消警报管理器的警报。然后,我们将结果置于待处理的意图中,以通过警报管理器访问它并应用取消。
Intent intent = new Intent(this, AlarmService.class);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
答案 1 :(得分:1)
private void cancelTimers() {
if (intent1 != null) {
timer1 = 0;
intent1 = null;
stopService(intent1);
text1.setText("+");
}
}
更改此代码
答案 2 :(得分:1)
停止服务不会停止onGoing警报必须以这种方式停止
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);