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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:46:03+00:00 2026-05-19T01:46:03+00:00

I have an activity with a checkbox: if the chekbox is unchecked then stop

  • 0

I have an activity with a checkbox: if the chekbox is unchecked then 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 a parameter “user_stop” to say at the service that has been a user to stop it and not the system (for low memory).

now I have 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 doesn’t work! I can’t use getIntent() in onDestroy!

any suggestion?

thanks

Simone

  • 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-19T01:46:04+00:00Added an answer on May 19, 2026 at 1:46 am

    I see two ways of doing this:

    1. Using Shared Preferences.
    2. Using local broadcasts.

    The first approach is an easy and straightforward way. But it is not very flexible. Basically you do:

    • a. Set “user stop” shared preference to true.
    • b. Stop service
    • c. In you service in onDestroy check what is the value of “user stop” preference.

    The other approach is a better way but requires more code.

    • a. Define a string constant in you service class:
    final public static string USER_STOP_SERVICE_REQUEST = "USER_STOP_SERVICE".
    • b. Create an inner class BroadcastReceiver class:

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

      registerReceiver(new UserStopServiceReceiver(),  newIntentFilter(USER_STOP_SERVICE_REQUEST));
    • d. From any place you want to stop your service:

      context.sendBroadcast(new Intent(USER_STOP_SERVICE_REQUEST));

    Note that you can pass any custom arguments through Intent using this approach.

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

Sidebar

Related Questions

I have this code for an expandable list, I want to have a checkbox
This is my situation. What i have: an activity with a checkbox(called checkAll) at
I have an activity with the following layout. So in this screen, initally the
I have a checkbox on my activity. I'm wondering how to make its state
Without using listactivity, in a simple activity how can I have checkbox at the
I have an Activity (not a ListActivity, although I should probably convert my code
I have the following code in Main.java : public class Main extends Activity implements
I'm new to Android. I have two activities. First activity shows selected contact list
I have this simple activity which suppose to change the text of TextView once
Basically I want to have a button to start a new activity after login.

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.