Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4558784
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:41:47+00:00 2026-05-21T17:41:47+00:00

I am trying to start a Service from my PreferenceActivity. I want to start

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T17:41:48+00:00Added an answer on May 21, 2026 at 5:41 pm

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to start a windows service which I have created from another windows
I am trying to start service from AsyncTask but cant see that its starts.
I'm trying to start SQLServer2008SP1 express installer from the NT service process. Process is
I'm trying to start a service from my activity like this: startService(new Intent(MyActivity.this, MyService.class));
I want to start service from fragment from a list view item. I am
I'm trying to start a Windows service on Windows Server 2003 from an ASP.NET
Hello I am trying to start the android browser from a service using this
I am trying to start the service from adb shell. There already is similar
I'm trying to start a service after the device has been started. The problem
I am trying to start a process programatically from windows service written in C#.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.