Reputation: 21
I think there is a simple problem in my code but I couldn't figure it out. I have a HeaderService which shows a bubble popup over some particular apps. Now what I want is stop that service when user presses Home key or Back key using BroadCastReceiver. Please take a look at my code and suggest me some solution. Thanks in Advance!!
This is my Receiver class
public class PopupClosingReceiver extends BroadcastReceiver {
private boolean isMyServiceRunning(Context con) {
ActivityManager manager = (ActivityManager) con.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (HeaderService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
@Override
public void onReceive(Context context, Intent intent) {
if(isMyServiceRunning(context)){
Intent serviceIntent = new Intent(context, HeaderService.class);
context.stopService(serviceIntent);
}
}
AndroidManifest.xml
<receiver android:name=".PopupClosingReceiver">
<intent-filter>
<category android:name="android.intent.category.HOME"/>
</intent-filter>
</receiver>
Upvotes: 1
Views: 1222
Reputation: 121
You can access the service at any time because the service is registered in the system ("Thread") wait to be activated or stops at any time.
Example:
public class Phonecall extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent song_wweather= new Intent(context,Song_weather.class); //service class
if(intent.getAction()=="android.intent.action.PHONE_STATE"){
TelephonyManager telephonyManager =
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener callStateListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber)
{
if(state==TelephonyManager.CALL_STATE_RINGING){
context.stopService(song_wweather);
}
if(state==TelephonyManager.CALL_STATE_IDLE){
context.startService(song_wweather);
}
}
};
telephonyManager.listen(callStateListener,PhoneStateListener.LISTEN_CALL_STATE);
}}
}
Upvotes: 1
Reputation: 121
public void sendSMS {
ActivityManager am= (ActivityManager) context.getSystemService( Activity.ACTIVITY_SERVICE );
List<ActivityManager.RunningServiceInfo> rs = am.getRunningServices(50);
String message = null;
for (int i=0; i<rs.size(); i++) {
ActivityManager.RunningServiceInfo rsi = rs.get(i);
Log.i("Service", "Process " + rsi.process + " with component "
Upvotes: 0