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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:02:42+00:00 2026-05-17T22:02:42+00:00

I have an activity that starts a service like that: Intent youtubeIntent = new

  • 0

I have an activity that starts a service like that:

    Intent youtubeIntent = new Intent(this, YoutubeFeedService.class);
    service = startService(youtubeIntent);

and to detect when the service stops I use a broadcast receiver:

@Override
public void onResume() {
    IntentFilter filter; 
    filter = new IntentFilter(YoutubeFeedService.NEW_VIDEO_CELL); 
    receiver = new FeaturedReceiver(); 
    registerReceiver(receiver, filter);
    super.onResume();
}

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



public class FeaturedReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String title = intent.getStringExtra("title");


        if (title.equals("-1") || title.equals("1")){
            //stopService(new Intent(FeaturedActivity.this, service.getClass()));
            try {
                Class serviceClass = Class.forName(service.getClassName());
                stopService(new Intent(FeaturedActivity.this, serviceClass));
            } 
            catch (ClassNotFoundException e) {}

        }

    }
}

I first tried to kill the service with

stopService(new Intent(FeaturedActivity.this, service.getClass()));

but this didn’t work, so instead I used

try {
                Class serviceClass = Class.forName(service.getClassName());
                stopService(new Intent(FeaturedActivity.this, serviceClass));
            } 
            catch (ClassNotFoundException e) {}

And it did work! Can anyone explain what’s the difference?

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-17T22:02:43+00:00Added an answer on May 17, 2026 at 10:02 pm
    stopService(new Intent(FeaturedActivity.this, service.getClass()));
    

    In this case, service is a ComponentName. Hence, service.getClass() will return ComponentName.class. Your service is YoutubeFeedService.class.

    Class serviceClass = Class.forName(service.getClassName());
    stopService(new Intent(FeaturedActivity.this, serviceClass));
    

    It would seem simpler to call:

    stopService(new Intent(FeaturedActivity.this, YoutubeFeedService.class);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a service, that starts an activity like this: Intent i = new
I have 4 buttons that when click starts a new activity class. This works
I have an single Activity application that starts a Service and binds to it.
So i have a service, that starts an activity displayed as a Popup thank's
I have an activity that starts after a certain time. Once this activity starts,
I have an Android application. In one activity I start a service like this:
So I have a very simple android activity that starts a timer when you
I have an Activity with a button that starts a long running task. I
I have an android activity and a service implemented using aidl. Works like a
I have the Activity and the Service. When Activity is started, it calls startService()

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.