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

  • Home
  • SEARCH
  • 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 9255495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:43:49+00:00 2026-06-18T11:43:49+00:00

I am writing an android app that when I run it first time it

  • 0

I am writing an android app that when I run it first time it works fine, but when I try to run it a second time it become shaky. I think maybe a thread or a service that I have started first time still continues to work and the second time that I start the app there will be a conflict or something.
In my app I have a main activity from which I start a service, and inside the service I start a thread that runs.
What are the general guidelines to follow when exiting an Android app. What are the specific things that one has to make sure it is done so after exit nothing stays running and make sure app does not hold some resources, in other words it is a clean exit.

Here is more details on my application:
My main activity is like this:

public class MainActivity extends Activity implements OnClickListener {
...
   public void onClick(View src) {
    switch (src.getId()) {
    case R.id.buttonStart:
        if (isService == false)  {
            Intent intent1 = new Intent(this, MyService.class);
            startService(intent1);
        }
        isService = true;
        if (firstTime == false) myService.setA(true);
        firstTime = false;
        break;
    case R.id.buttonStop:
        if (isService == true)  {
            Intent intent1 = new Intent(this, MyService.class);
            myService.setA(false);
            stopService(intent1);
        }
        isService = false;
        break;
    }
   }

   ...
}

And my service looks like this:

public class MyService extends Service {
private boolean a=true;
...

@Override
public void onCreate() {    
    super.onCreate(); 
    int icon = R.drawable.icon;
    CharSequence tickerText = "Hello";
    long when = System.currentTimeMillis();
    Notification notification = new Notification(icon, tickerText, when);
    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.setLatestEventInfo(this, "notification title", "notification message", pendingIntent);     
    startForeground(ONGOING_NOTIFICATION, notification);
    ...
}

@Override
public void onDestroy() {
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onDestroy");
}

@Override
 public int onStartCommand( Intent intent, int flags, int startId ) {
    Thread mythread = new Thread() {
        @Override
        public void run() {
            while(a)
            {
                PLAY AUDIO
            }
        }
    };
    mythread.start();
    return super.onStartCommand( intent, flags, startId );
}

public void setA(boolean aa) {
    Log.d(TAG,"a is set");
    this.a = aa;
}
....
}
  • 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-18T11:43:51+00:00Added an answer on June 18, 2026 at 11:43 am

    Always clean up resource when you don’t need them. For example: if the Service is only needed during the runtime of your Activity -> call stopService in your Activity.onPause. (and startService in Activity.onResume).

    About your Service, does it need to keep running. Or should it do 1 task and then it is done? If so use IntentService, which will shut itself done when there are no more Intents to process.

    Furthermore what kind of thread are you using? Thread or AsyncTask or something else? Thread is pretty basic, a suger version such as AsyncTask may do the job better. Very depending on what your are doing with it.

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

Sidebar

Related Questions

I'm currently writing an app in Android that works with the GPS. At the
I'm writing an Android app that communicates with a website. Any time I hit
Im writing an android app that connects to my own Jersey rest client. HTTP
I am currently writing an Android app that, among other things, uses text information
I'm writing an Android app that needs to access GMail, and I'd like to
So I'm writing an android app that needs to grab book price data from
I'm new to android and am writing a simple app that calculates the distance
i'm writing an Android app and i've run into a bit of a roadblock
I am writing an Android app where I am trying to run the Facebook.authorize
I'm writing an Android application that should upload photos to Facebook. Everything works just

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.