Question

I am trying to start a Service from my PreferenceActivity. I want to start a service on every onPreferenceChangeListenr.

I have four conditions in my onPreferenceChangeListener and each condition has to start same Service with different values.

When i change the preference for the first time it starts the service and when i change the preference second time it starts the service with new value but it does not stop the execution of the previous condition so now i have a service which is producing results for two conditions and if i change the preference third time, it starts producing result for three condition but what i need is to run this service only for the current condition.

Here is my PreferenceActivity's code:-

public class Preferences extends PreferenceActivity
{
private Button button;
ListPreference lp;
private Context context;
private long duration;

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
    context = this;
    lp = (ListPreference)findPreference("autoduration");
    final Intent intent = new Intent(context, BackService.class);
    lp.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener()
    {
        public boolean onPreferenceChange(Preference preference, Object newValue)
        {
            String newSelection = newValue.toString();
            int index = lp.findIndexOfValue(newSelection);
            stopService(intent);
            if(index==0)
            {
                duration = 15000;
            }
            else if(index==1)
            {
               duration = 12000;
            }
            else if(index==2)
            {
                duration = 10000;
            }
            else if(index==3)
            {
                IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
                filter.addAction(Intent.ACTION_SCREEN_OFF);
                BroadcastReceiver receiver = new ScreenReceiver();
                registerReceiver(receiver, filter);
            }
            intent.putExtra("duration", duration);
            startService(intent);
            return true;
        }
    });


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

And its my service code which should be affected by my PreferenceActivity. :-

private static Timer myTimer = new Timer();
public static final String TAG = "BackService";
private MainTask mainTask = new MainTask();    

 public void onStart(Intent intent, int startId)
    {
        Long duration = intent.getLongExtra("duration", 7000);
        startServices(duration);
    }

public void startServices(Long dur)
    {
        myTimer.scheduleAtFixedRate(mainTask,0,dur);
    }
private class MainTask extends TimerTask
    {
        @Override
        public void run() {
            Log.e(TAG, "I am HERE");
        }
    }

Here is my BroadcastReceiver :-

public class ScreenReceiver extends BroadcastReceiver {
public static boolean screenOn = true;
private static final String TAG = "ScreenReceiver";

@Override
public void onReceive(Context context, Intent intent)
{
    if(intent.getAction().equals(Intent.ACTION_SCREEN_ON))
    {
        Log.e(TAG, "I am receiver");
    }
}
}

I don't know what i am doing wrong so Please Help!!

Thanks

Was it helpful?

Solution

Your service may be getting restarted without you realizing. However, you have a problem because in that case, the myTimer is not getting torn down. You need to implement the onDestroy method in your class, determine if your service is getting stopped (because somewhere stopSelf or Context.stopService is being called).

If so, you should remove the static modifier from the myTimer variable, so that it doesn't hang around between starts of the service and in the onDestroy method you need to call myTimer.cancel() to stop it, since otherwise it will just hang around and you won't have any reference to it to cancel it.

In your startServices method, add the following before the call to myTimer.scheduleAtFixedRate:

mainTask.cancel();
mainTask = new MainTask();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top