Stream Major
Stream Major

Reputation: 37

Keep my thread running when starting new intent

Here's the thing, I want to keep my thread running (it's downloading a file) when I start an intent (a media player to play what is downloading).

The problem is when I called my intent, the onPause method is automatically called.

So what should I do to avoid that ?

    public class MyActivity extends Activity {

      MyThread my_thread;

      public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        //Starting My thread
        my_thread = new MyThread();
        my_thread.start();

        // Starting the intent 
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.setDataAndType(uri, "video/*");
        startActivity(intent);

      @Override
      protected void onPause() {
        super.onPause();
        // Stop my thread
        my_thread.interrupt();

       } 
}

EDIT : I'm downloading a live stream so there is no end of it unless the user decide to close the activty.

Upvotes: 0

Views: 421

Answers (3)

gunar
gunar

Reputation: 14710

You can try one of:

  1. (For api level 9 above): Use DownloadManager to download the file and when you're done send a broadcast to notify that you've finished downloading and the Activity can use the file downloaded. These two articles (this and this) should help you out.
  2. If you're developing for Froyo as well, a very similar approach: you can start an IntentService (that is doing its work on a serial background thread), download the file and when finished you'll send a broadcast intent containing the path where the file can be found. Here are two articles to help you with IntentServices (this and this)
  3. Or you can use an AsyncTask.

For 1 and 2: In your activity you'll have to register a BroadcastReceiver that will handle above intents. Something like:

public class MainActivity extends Activity {

    protected BroadcastReceiver myDownloadCompleteReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if ("my_package.actions.DOWNLOAD_COMPLETE".equals(intent.getAction())) {
                String filePath = intent.getStringExtra("DOWNLOAD_PATH");
                // use the filePath
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        IntentFilter filter = new IntentFilter("my_package.actions.DOWNLOAD_COMPLETE");
        LocalBroadcastManager.getInstance(this).registerReceiver(myDownloadCompleteReceiver, filter);
    }

}

Upvotes: 0

JohanShogun
JohanShogun

Reputation: 2976

You may want to take a look at AsyncTasks instead, they allow better multi threading on Android for these sort of things.

A quick example could look like this (from http://developer.android.com/reference/android/os/AsyncTask.html):

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
             // Escape early if cancel() is called
             if (isCancelled()) break;
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }

Edit: didn't see it was a continuous stream, don't use the above code. Maybe you should put that in your original question?

Upvotes: 2

Cristiano Zambon
Cristiano Zambon

Reputation: 466

The OnPause event of a class extending Activity is always called when you start a new Activity, that is what you are doing with startActivity(intent).

If you want that your thread stop downloading data when you exit the application, that's what I think is the meaning of your myThread.interrupt() into the OnPause callback, do not use "OnPause" but choose another event. You can find a nice description of what events are started when here: Activity lifecycle

Upvotes: 0

Related Questions