0

now i have a server class that i run on a thread from an activity(i.e servActivity).now when i am not interacting with my app in anyway possible (like i have removed it from recent apps etc) the thread should stop which currently is not stopping. So i researched and i found that i should use a bound service. now a bound service i will have to bind it to servActivity and when servActivity is destroyed i have to unbind and stop service but i dont want to do that. i want to stop service when i am not interacting with the app. i also found that maybe i have to extend application class but cannot find the solution to achieve this?Is it advisable to extend the application class?

i want to be able to create a service running on independent thread from a particular activity(ie servActivity) and then be able to interact with the service from any activity and service should be active (even if the activity in which i started the service i.e-servActivity is destroyed by going to previous activity etc) through button or whatever until i am not interacting with the app(i have a notification controller which also needs to be closed to stop the interaction)

i have a client class on one device whose object i create again and again if i have to make request but i want to make only one object for server class because it has a while(true) loop so it keeps running so i want to be able to interact with the server from all activities and stop it when i am not interacting with the application

i also found a way in which i can make an abstract class which extends activity and extend that derived class to all the other activities in my app.But how to i bind the service to all the other activities in the class so that i can interact with the service from all the other activities?And how would i know that if all activities and notification controller have been stopped and there is no interaction with user?something like this how to know our app has gone to background in android

If there is there any other method please suggest

Please help thanks in advance

Community
  • 1
  • 1
user rk
  • 376
  • 5
  • 13

1 Answers1

1

You can create a BroadcastReceiver in your Service Class to interact/start/close your Service from any Activity or even from any App.

Your Activities can broadcast custom Action Strings which can be picked up by any BroadcastReceivers (even ones set up in Services) and thereby invoking their onReceive() methods allow communication.

1) I suggest you don't bind your Service to any Activity and instead use Intent to initiate it in your Activity like this....

//In your Activity
Intent i = new Intent(this, /*MyServiceClassName.class*/);
startService(i);

Or else your Service may still be active until you unbind it.

2) Create a BroadcastReceiver in your Service Class to listen for certain Action Strings broadcasted by your Activities....

//In your Service
private final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
  String action = intent.getAction();
  if(action.equals(/*"Action String to stop Service"*/)){
    stopSelf();
  }else if(action.equals(/*"Action string to interact with Service"*/)){
   //Do what you want
    };

3) Now set what Action Strings the Broadcast Receiver will listen for and also register it in your Service onCreate() method....

//In your Service onCreate() method 
IntentFilter filter = new IntentFilter();
filter.addAction(/*"Action String to stop Service"*/); 
filter.addAction(/*"Action String to do something"*/); 

registerReceiver(receiver, filter);

4) And also unregister your receiver when Service onDestroy() is invoked as housekeeping....

//Service onDestroy()
@Override
public void onDestroy(){
super.onDestroy();
unregisterReceiver(receiver);
}

5) Finally broadcasting Action Strings from your Activities through Intent....

//From any Activity
Intent intent = new Intent(/*"your custom Action String that should match
               up with whats set up with the BroadcastReceiver in Service"*/);
sendBroadcast(intent);

6) So once the broadcast is sent your receiver should pick it up then its onReceive() method will be invoked. Therefore you now have a medium for your Activities and Service to communicate through and also the Service will persist even after you close your app until you stop it explicitly with....

//From any Activity
Intent i = new Intent(this, /*MyServiceClassName.class*/);
stopService(i);

7) Stop service when app is stopped....

//In all your activities
@Override
protected void onDestroy() {
    Intent i = new Intent(this, /*MyServiceClassName.class*/);
            stopService(i);
    super.onDestroy();
}

8) First you'd need to put a killcode intent action String in your Service as demonstrated in points 2 and 3 then put this code in your app's Activity onPause() methods....

@Override
protected void onPause() {
    PendingIntent pintent = PendingIntent.getBroadcast( this, 0, new Intent(/*"Action String to stop Service"*/), 0 );
    AlarmManager manager = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));

    // set alarm to fire 10mins (1000*60*10) from now (SystemClock.elapsedRealtime())
    manager.set( AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 1000*60*10, pintent );
    super.onPause();

}

And this in your app's Activity onResume() methods....

@Override
protected void onResume() {
    PendingIntent pintent = PendingIntent.getBroadcast( this, 0, new Intent(/*"Action String to stop Service"*/), 0 );
    AlarmManager manager = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));
    manager.cancel(pintent);
    super.onResume();

}
TheArbiter
  • 11
  • 3
  • but how do i stop the service when i exit the application because i dont want the tcp server class to keep open even after the app is removed from background. how do i know which activity is in the background or application is exiting? i also want to be able to stop the service if the application is in the background for maybe let say more than 10 min.BTW thanks for the solution i ll accept the answer as i try it in 4-5 days(i hope u dont mind). And if the answer of the above question is big please add it to the above solution. – user rk Dec 11 '15 at 13:35
  • @user Check out points 7 and 8 in above. I've now numbered the blocks in my answer. Hopefully all goes well.... – TheArbiter Dec 12 '15 at 07:27