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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:39:18+00:00 2026-05-25T06:39:18+00:00

Is there a way for me to create a service to track my activity

  • 0

Is there a way for me to create a service to track my activity class and restart it after a crash? Note that i CANNOT use uncaughthandlers thread method to restart my app. My app is supposed to crash, don’t worry about that part. My app is something simple, like this

private class AudioRenderer extends Activity {

private MediaPlayer AudioRenderer(String filePath) {
File location = new File(filePath);
        Uri path = Uri.fromFile(location);
  mp= MediaPlayer.create(this, path); 

}
return mp


}

Once this crashes, the service listening in the background will restart my app automatically. Anybody knows how this is possible? 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-25T06:39:18+00:00Added an answer on May 25, 2026 at 6:39 am

    You can do that, yes, as explained below. But if such techniques may make sense for experiments, they are definitely not suitable for production. That would be awfully ugly and unefficient.

    This said, here is a way to go:

    • Make your Service sticky or redelivered to ensure it will always be running after having been started once and not explicitely stopped.

    • in your Activity class, statically store WeakReferences pointing to all its running instances and provide a way to statically check whether at least one of them is currently allocated:

      public class MyActivity extends Activity {
          private static ArrayList<WeakReference<MyActivity >> sMyInstances = new ArrayList<WeakReference<MyActivity >>();
      
          public MyActivity() {
              sMyInstances.add(new WeakReference<MyActivity >(this));            
          }
      
          private static int nbInstances() {
              int ret = 0;
              final int size = sMyInstances.size();
      
              for (int ctr = 0; ctr < size; ++ctr) {
                  if (sMyInstances.get(ctr).get() != null) {
                      ret++; 
                  }
              }
      
              return ret;
          }
      }
      

    (WeakReference are references to objects that do not prevent these objects to be garbage-collected, more details here)

    • Then, from your Service, call MyActivity.nbInstances() from time to time. It will return 0 a (usually short but theoretically unpredictable) while after the crash of the last running MyActivity instance. Warning: it will do so unless you have a memory leak concerning this Activity or its underlying Context as this leak would prevent the garbage collection of the instance that crashed.

    • Then you just have to start a new instance of your Activity from your Service, using startActivity(Intent)

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

Sidebar

Related Questions

Is there a way in Android (starting with API 8) to create a service
Is there a way to create a callback function that fires when a list
Is there a way to create a service using C# able to check if
I want to create a service that has a onWindowFocusChanged method, native to Activity.
hello is there a way to create soap service with rails3 application. I have
In a REST WCF Service, is there a way to create a custom WebFaultException?
Is there any way to create a background service (or driver or anything else)
Is there a way to create a layout like the notification bar (the sliding
Is there any way to create a dynamic Pinterest button? I'm trying to add
Is there a way to create a StringBuilder from a byte[] ? I want

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.