stopself()服务无法正常工作

时间:2016-06-18 23:33:16

标签: android service

我需要停止服务,我使用stopSelf方法,但服务没有停止。

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    int hour = MainActivity.sa3a;
    int minute = MainActivity.dakika;
    java.text.DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date = new Date();
    System.out.println(dateFormat.format(date));
    int x = date.getHours();
    int y = date.getMinutes();
    String n = String.valueOf(x);
    Toast.makeText(send.this,n, Toast.LENGTH_SHORT).show();
    if (hour == x && minute == y) {
        Toast.makeText(send.this, "start", Toast.LENGTH_SHORT).show();
        send.this.stopSelf();
        Toast.makeText(send.this, "stop_Self_is_Not_work", Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(send.this, "yOKKKKKK", Toast.LENGTH_LONG).show();
    }

    return START_STICKY;
}

1 个答案:

答案 0 :(得分:1)

为了看到该服务确实停止,您需要覆盖onDestroy()回调。 想要在onDestroy方法中显示一个toast,然后当你调用stopSelf()时你的toast应该显示。

如果未显示当前状态的吐司,则表示不满足条件。如果您只从该条件调用stopSelf,则不会停止该服务。您需要进一步检查您的条件,以保证在您希望的地方停止服务。

相关问题