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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:46:21+00:00 2026-05-23T09:46:21+00:00

I have an activity which calls a service through startService(new android.content.Intent(this,NeglectedService.class); I am trying

  • 0

I have an activity which calls a service through

startService(new android.content.Intent(this,NeglectedService.class);

I am trying to add a ToggleButton, that while isChecked(True) the service will run, and vice versa.

        toggleService = (ToggleButton)findViewById(R.id.btnStopStartService);
    toggleService.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            if(toggleService.isChecked())
            {
                // Start the service
                showToast("true");
                startService(new Intent(this,NeglectedService.class));
            }
            else
            {
                // Pause/Stop the service
                showToast("false");
            }
        }
    });

But calling this way returns an error, I don’t know how to tackle

The constructor Intent(new View.OnClickListener(){}, Class<NeglectedService>) is undefined

If I remove the new Intent(this, NeglectedService.class) than the error dissappears, but the service is not called (ofc, the activity doesn’t know who to call)

So, my question:
– How can I monitor and start/stop a service from my activity?

Question: How can I control a service state from the activity that created it?
I have a togglebutton.onclicklistener. I want that when the toggle is ON, the service will run, and stop wehn toggled to off.
I will use sharedpreferences to store the service state, but I get an error when calling startService(new android.content.Intent(this,NeglectedService.class); from the if(toggleService.isChecked())

  • 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-23T09:46:21+00:00Added an answer on May 23, 2026 at 9:46 am

    The constructor that you are trying to use takes a Context as its first argument, but this isn’t an instance of Context, it’s the anonymous on-click listener that you are declaring. To pass the outer instance you need to qualify it:

    startService(new Intent(MyActivity.this, NeglectedService.class));
    

    where MyActivity is the name of your activity class.

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

Sidebar

Related Questions

I have a workflow which has a custom activity which calls a web service.
I have question regarding the service and activity. I have one service which calls
I have one activity 'A' which extends mapActivity which calls service 'B', and Service
Hi I have an Activity(Activity_1) which calls another Activity(Activity_2) using activity for result. In
I have an activity which needs to make two remote server calls. The first
I have an Android activity which in turn starts a thread. In the thread
I have an Activity (A) and a Service (S) which gets started by A
My app makes several calls to getFromLocationName from the android.location.Geocoder; class, this is currently
In my Android application I have an Observable Data class, which contains a method
I have a receiver which listens to incoming calls. From this receiver i am

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.