Android: How do I access AsyncTask from the PendingIntent created by the status bar notification?

My application launches AsyncTask, which downloads the file from the url. At the same time, it creates a status bar Notificationthat tells the user the percentage of completion of the download.

I am trying to configure the application to click on a notification. If the download is still in progress, I want to show DialogInterfacewhich asks them if they want to stop the download. Click "Yes" to stop the download.

The problem I am facing is that I'm not sure how to access my Async task from PendingIntentwhich I configured for notification. I can get DialogInterfaceto show easily enough, but I'm not sure how to show the action that occurs when the download should stop it.

I tried to create a Helper class that had access to the notification, as well as a File object that refers to the downloaded file, but I get an error that the object is not serializable (it implements Serializable). The helper class also included an element that would track the progress of loading, and this is what I would like to use to condition whether to show a dialog or not.

I was thinking about using the Brodcast action and receiver, but I'm not sure where to place the receiver. Will he go in a class that expands AsyncTask?

Any help would be greatly appreciated. This PendingIntentis attached to Notification. If you want to see more code, just ask.

public class DownloadNotificationActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate( Bundle savedInstanceState ) {
    super.onCreate( savedInstanceState );

    Intent i = getIntent();

    DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

        @Override
        public void onClick( DialogInterface dialog, int which ) {
            switch ( which ) {
                case DialogInterface.BUTTON_POSITIVE :
                    // Yes button clicked
                    // Stop download
                    finish();
                    break;

                case DialogInterface.BUTTON_NEGATIVE :
                    // No button clicked
                    finish();
                    break;
            }
        }
    };

    if ( /* download not complete */ ) {
        AlertDialog.Builder builder = new AlertDialog.Builder( this );
        builder.setMessage( R.string.stop_download ).
                setPositiveButton( R.string.yes, dialogClickListener ).
                setNegativeButton( R.string.no, dialogClickListener ).show();
    }
    else {
        // Access file
    }

}
}

, , ViewDetailActivity. DownloadFile, AsyncTask , . doInBackground() DownloadFile URL- URL-, Notification . PendingIntent Notification DownloadNotificationActivity ( ), , "" , AsyncTask.

, DownloadFile, , , DownloadFile DownloadNotificationActivity, .

!

+5
1

asynctask isCancelled() doInBackground() . cancel() asynctask.

asynctask .

:

AsyncTask :

class MyAsyncTask extends AsyncTask<Void, Integer, Void> {
        @Override
        protected Void doInBackground(Void... unused) { //be sure to check isCancelled here, i.e. if (isCancelled()) break;

Activity asynctask :

public class MyActivity extends Activity {
    private MyAsyncTask zeTask;
    @Override
    public void onCreate(Bundle savedInstanceState) {
              zeTask= new MyAsyncTask();
              zeTask.execute();

, async, zeTask. MyAsyncTask , , zeTask , AsyncTask.

switch ( which ) {
                case DialogInterface.BUTTON_POSITIVE :
                    zeTask.cancel();
                    finish();
                    break;
+1

All Articles