私は数秒ごとに何かを実行し、一定時間が経過するまでスリープするサービスを持っています。理想的ではありませんが、私の目的には効果的です。私は、NotifActivityの開始のためのレイアウトにボタンを持っていて、NotifServiceと呼ばれるサービスを要求したいと思っています。ここで私はNotifServiceに持っているものは基本的である:レイアウト内の他のアクティビティのボタンが押されたときにサービスを停止する方法
public class NotifService extends IntentService {
public NotifService() {
super("NotifService");
}
@Override
protected void onHandleIntent(Intent intent) {
while(true){
try {
checkcalc();
Thread.sleep(1000);
} catch (InterruptedException e) {
// Restore interrupt status.
Thread.currentThread().interrupt();
}
}
}
public void checkcalc() {
//some code I am trying to run
}
public void notifSend(String num){
//Sends a notification
}
}
、サービスを開始するために呼び出されNotifActivityのビット:
public void sendNotification(View view) {
Intent serviceIntent = new Intent(this, NotifService.class);
startService(serviceIntent);
}
どのように私は今、サービスを停止するのでしょうか? stopService(serviceIntent)は効果がないようですので、何をしなければなりません。
ありがとうございます!
"* stopService(serviceIntent)は無効と思われます*" ...なぜですか? – Shaishav
ボタンをトリガーとするメソッドの中にそのメソッドを配置しても、通知は定期的に受信されるため、通知サービスは停止していないようです。 –