How to pass parameter from activity to service ... when user stops service

I have an action with a checkbox: if chekbox is not installed, stop the service. this is a snippet of my activity code:

Intent serviceIntent = new Intent(); serviceIntent.setAction("com.android.savebattery.SaveBatteryService"); if (*unchecked*){ serviceIntent.putExtra("user_stop", true); stopService(serviceIntent); 

when I stop the service, I pass the parameter "user_stop" to say in the service that was the user, to stop it, and not the system (for low memory).

now I need to read the variable "user_stop" in void onDestroy of my service:

 public void onDestroy() { super.onDestroy(); Intent recievedIntent = getIntent(); boolean userStop= recievedIntent.getBooleanExtra("user_stop"); if (userStop) { *** notification code **** 

but it will not work! I can not use getIntent () in onDestroy!

any suggestion?

thanks

Simone

+3
source share
4 answers

I see two ways to do this:

  • Using general settings.
  • Using local gears.

The first approach is a simple and understandable way. But it is not very flexible. Basically you:

  • a. Set Custom Stop to true.
  • b. Service stop
  • from. In your onDestroy service, check what the user-stop preference value is.

Another approach is a better way, but requires more code.

  • a. Define a string constant in your service class:
 final public static string USER_STOP_SERVICE_REQUEST = "USER_STOP_SERVICE". 
  • b

    . Create a class class BroadcastReceiver:

     public class UserStopServiceReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //code that handles user specific way of stopping service } } 
  • from. Register this receiver in the onCreate or onStart method:

     registerReceiver(new UserStopServiceReceiver(), newIntentFilter(USER_STOP_SERVICE_REQUEST)); 
  • d

    . From any place where you want to end your service:

     context.sendBroadcast(new Intent(USER_STOP_SERVICE_REQUEST)); 

Note that with this approach, you can pass any user arguments using Intent.

+6
source

I don't think relying on onDestroy () is good. There are several approaches you can take.

+1
source

I have the same setup in my application using CheckBoxPreference in my activity and getSharedPreference in the onCreate service class. When checked, the service starts. Uncheck the box to stop the service.

Listen and process click preferences in my activity:

  getPreferenceManager().findPreference("prefEnable").setOnPreferenceClickListener(new OnPreferenceClickListener() { // not used: Intent intent = new Intent(this, MyService.class); @Override public boolean onPreferenceClick(Preference preference) { CheckBoxPreference cb = (CheckBoxPreference) preference; if (cb.isChecked()) { Log.d(TAG, "User enabled service"); // code to start service } else { Log.d(TAG, "User disabled service"); // code to stop service } return true; } }); 

Get my prefEnable preference in my onCreate service:

  SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); Boolean isEnabled = preferences.getBoolean("prefEnable", false); 

Perhaps in your onDestroy you can reuse this and say:

 if (isEnabled==false) { *** notification code *** 
-1
source

// You can pass this parameter as follows: -

 Intent serviceIntent = new Intent(this,ListenLocationService.class); serviceIntent.putExtra("From", "Main"); startService(serviceIntent); //and get the parameter in onStart method of your service class @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); Bundle extras = intent.getExtras(); if(extras == null) Log.d("Service","null"); else { Log.d("Service","not null"); String from = (String) extras.get("From"); if(from.equalsIgnoreCase("Main")) StartListenLocation(); } } Enjoy :) 
-1
source

All Articles