3

I've a Fragment fr, Service ser, BroadcastReceiver brc.

In fr I have a switch, when checked it launches ser, when un-checked it stops it, so far so good.

In ser, I have registered a BroadcastReceiver named brc and added an IntentFilter named USER_PRESENT, When the device is locked, I have the Service call stopSelf() and register that BroadcastReceiver, when the phone is unlocked, onReceive() will be called, in that method I've added a code to start the Service again .

Now, the problem .. I can't stop the Service when the switch is un-checked, even though I'm calling stopService(intentname); .

And my guess is, it's a different Intent which launched by a different class. So I've stopped that Intent like this:

stopService(brc.intentname);

Nothing happened and the Service keeps running. What's the solution for this?.

Code:

Fragment:

if ( !Switched ) {
    getActivity().startService(ServiceIntent);
} 
if( Switched ){
    getActivity().stopService(ServiceIntent);
    getActivity().stopService(BroadcastTest.ServiceIntent);

}

Service:

if( !isScreenOn && isLocked )
{
    this.getApplicationContext().registerReceiver(new BroadcastTest(), new IntentFilter("android.intent.action.USER_PRESENT"));
    CancelTimer();
    stopSelf();
}

BroadcastTest:

ServiceIntent = new Intent(context, Service.class);
context.startService(ServiceIntent);
Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61
Mohammad
  • 41
  • 4
  • it's easier for people to help you if they have some [piece of code](http://stackoverflow.com/help/mcve) to work on - please show us what you have tried so far – Bö macht Blau Feb 01 '16 at 07:43
  • I've added the codes – Mohammad Feb 01 '16 at 07:48
  • I've thought about adding a boolean to check if Service Switch is enabled or not, if not it will stopself, But i'd like to know why it's not stopping . – Mohammad Feb 01 '16 at 07:49
  • Yes____________(for length policy) – Mohammad Feb 01 '16 at 07:53
  • Personally, I always call "startService()" with an Intent with an action like "my.package.name.STOP" and then evaluate the action in "onStartCommand()" and let the service call "stopSelf()". Now I just found this [answer](http://stackoverflow.com/a/9665584/5015207) to a similar problem which shows I'm not the only one to do so (very reassuring :) ) – Bö macht Blau Feb 01 '16 at 07:57
  • And I think you may be right with your guess about the different Intents. – Bö macht Blau Feb 01 '16 at 07:59
  • Please check exactly the content of the `Intent` that you pass to `stopService()`. Add logging that logs the exact content of this `Intent`. I'm guessing that the `Intent` doesn't contain what you think it should contain. – David Wasser Feb 01 '16 at 15:26
  • DavidWasser Can you please explain a bit more ?, @0X0nosugar I'm gonna try you way and see, I'm sure it will work, But i'd like to know why the service isn't stopping, And another thing, How about i send a broadcast to "BroadcastTest" with action, and check if the action "stop", i can stop it from the BroadcastTest, rather than stopping it from the fragment which didn't work. – Mohammad Feb 01 '16 at 17:55
  • Just saw this http://stackoverflow.com/questions/22100189/android-stop-a-service-from-a-different-class?rq=1 , looks promising. – Mohammad Feb 01 '16 at 17:57
  • Update, I have switched the way to start the service, I've sent a broadcast to BroadcastTest with "start" action, and then the service starts, and when un-checked, it will send a "stop" action, all good so far, but the damn service is still running! I'm starting and stopping the same service, as someone said abot services, i'm requesting to be stopped, not force to stop, maybe that's the reason ? Or it's registering / un-registering broadcast problem ? – Mohammad Feb 01 '16 at 18:48
  • How do you know your `Service` is still running? – David Wasser Feb 02 '16 at 09:55
  • Post the code from your `onStartCommand()` – David Wasser Feb 02 '16 at 09:56

0 Answers0