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);