我该如何停止服务?

时间:2016-03-29 16:08:14

标签: android

我有一个显示祝酒消息的服务。我在清单中定义了它 -

 <service android:name=".ShowNotification" />

这是我的服务 -

public class ShowNotification extends Service {
@Override
public void onCreate() {
    super.onCreate();
    Toast.makeText(getBaseContext(), "service worked once",
            Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

}

但我无法弄清楚如何停止此服务以及如何再次启动它?

2 个答案:

答案 0 :(得分:0)

尝试:

stopService(new Intent(this, ShowNotification.class));

startService(new Intent(this, ShowNotification.class));

重新启动服务,gona召回onCreate()

答案 1 :(得分:0)

使用IntentService 而不是仅使用服务。完成任务后,IntentService将自动销毁。您无需担心停止它。