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 8229945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:58:10+00:00 2026-06-07T16:58:10+00:00

I have an application that has a service to be conditionally started on boot

  • 0

I have an application that has a service to be conditionally started on boot (based on a checkbox preference defined in xml). My broadcast receiver class is this:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

public class BootServiceReceiver extends BroadcastReceiver {
private static final String TAG = "tweakmanager";

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
        SharedPreferences prefLights;
        prefLights = PreferenceManager.getDefaultSharedPreferences(context);
        if (prefLights.getBoolean("checkbox1", false)) {
            Log.i(TAG, "doing nothing...");
        } else {
            Intent p = new Intent();
            p.setAction("app.tweaks.Service");
            context.startService(p);
        }
    }
}
}

Note that service should only start if checkbox is unchecked. Default state is checked.

Now, on normal operation, all is working good – if checkbox is checked, service doesn’t start, if it’s unchecked, service starts on boot.

The problem lies if the application is installed but never run… In this case, service starts at next boot even if checkbox is checked. I thought about writing the default preferences to a file that could be read by the receiver, but I don’t think that’s the nicest solution…

  • 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-06-07T16:58:12+00:00Added an answer on June 7, 2026 at 4:58 pm

    In this case, service starts at next boot even if checkbox is checked

    It should not do so, as there will be no preference, and so your getBoolean() should return false as you specified.

    That being said, there is a better solution:

    1. Add android:enabled="false" to the manifest for this <receiver> element.

    2. When they toggle the checkbox in preferences, use PackageManager and setComponentEnabledSetting() to enable or disable the BroadcastReceiver, such that you are enabled when the checkbox is checked.

    This way, Android does not have to bother forking your process on a reboot, if the user has not checked this checkbox.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a application in Android that has a broadcast receiver that clear any
We have an application that has a WCF service (*.svc) running on IIS7 and
I have a wcf service application that has some application startup code in the
I've an application that has a widget already and have a service which updates
I have an application that has a Service that uses an ArrayList<Double> to store
I have a an application that has 2 parts. A service which creates content.
I have a Java EE application that has two components: First is a service
I have a .NET 2.0 application that has recently had contributions that are Service
I have an RMI application that has service implementation and it has a lot
I have a GWT application that has RPC service on its back end. I'm

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.