Question

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;
}
....
}
Was it helpful?

Solution

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.

OTHER TIPS

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

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

Thanks

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top