如何从另一个类中停止NotificationService?

时间:2017-06-11 20:31:24

标签: java android android-activity service

我有registerReceiver类扩展NotificationService并启动了service whit方法,但我不知道如何停止当前的服务,我阅读了一些指南和示例,但{{1}方法我无法阻止我的服务。这是我班上的代码,我怎么能停下来?

Service

2 个答案:

答案 0 :(得分:0)

这可能不是最好的方法,但我使用公共静态变量作为IntentService的标志。如果设置了标志,执行任何必要的清理,停止线程,退出循环,让它自然地流向stopSelf()和stopForeground()(你已经拥有)。

How to check if a service is running on Android?

答案 1 :(得分:0)

试试这个

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    timeStart = intent.getExtras().getLong("timeStart");
    es = (Esercizio) intent.getExtras().get("esercizio");

    while(System.currentTimeMillis() < timeStart + TIME_WAKE_UP);

    atWork();

    stopForeground(true);
    stopSelf();

    return START_NOT_STICKY;
}