![](/img/trans.png)
[英]Android - How can I check the sender class of the “stopService” Intent?
[英]how to use StopService for this Intent in Android?
我使用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;
}
}
謝謝。
在這里,我們通過調用警報服務從警報管理器中取消警報。 然后,我們將結果放入待處理的意圖中,以通過警報管理器對其進行訪問並應用cancel 。
Intent intent = new Intent(this, AlarmService.class);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
private void cancelTimers() {
if (intent1 != null) {
timer1 = 0;
intent1 = null;
stopService(intent1);
text1.setText("+");
}
}
更改此代碼
stopService不會停止onGoing警報,必須以這種方式停止
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.