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;
}
....
}
TJ1
  • 7,578
  • 19
  • 76
  • 119

2 Answers2

1

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.

RvdK
  • 19,580
  • 4
  • 64
  • 107
  • Thanks for the answer, I updated my question with more details. The purpose of the service is to play audio on the background so as long as user don't exit the app it should work. I am using `Thread` not AsyncTask. So based on my codes above what items do I need to take care of when user clicks on an exit button that I plan to put there? – TJ1 Feb 07 '13 at 16:15
  • Thread is fine here, you should move your Thread creation to OnCreate. OnStartCommand exits when the Intent is handled (=very quick). Therefore you have a dangling thread which keeps running. – RvdK Feb 08 '13 at 10:51
  • So should I move the whole thread creation, including `while(a) {...}` to onCreate, and just leave `mythread.start()` inside the `inStartCommand`? And how I can stop the thread from running? – TJ1 Feb 08 '13 at 14:28
  • Define the Thread in the object scope (where a is defined). You can stop it in the onDestroy – RvdK Feb 08 '13 at 14:59
  • You mean I put `Thread mythread;` after defining `a` and then create it in `onCreate`? Also is there `mythread.stop()` that I can put in `onDestroy`? If not how can I stop the thread there? Thanks a lot for all the help. – TJ1 Feb 08 '13 at 15:08
  • Correct, that should do it. Don't forget to accept the answer. – RvdK Feb 08 '13 at 15:09
0

Try Killing the process if there is no other complications.....

android.os.Process.killProcess(android.os.Process.myPid());

Thanks

ASP
  • 3,645
  • 1
  • 31
  • 45