Developing a shortcut for users to come back to the activity by clicking status showed in the notification bar

StackOverflow https://stackoverflow.com/questions/10967891

Question

Currently, I am trying to parse an online XML file and insert the data into database by using AsyncTask.

When the AsyncTask is performed, I will notify users the percentage of completion via ProgressDialog as well as notification bar.

Since the users may press the home button and visit other apps during the update process, I would like to develop a shortcut like that: When the users click the status showed in notification bar, they can directly come back to DataBaseUpdateService.

The following is the code I try to implement but fails to do so, may you give me advice on how to modify it?

DataBaseUpdateService

    public class DataBaseUpdateService extends Activity {

    Updatetask Task; 

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        Task = new Updatetask(this.getApplicationContext());
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
    }

    @Override
    public void  onPause()
    {super.onPause();}

    @Override
    public void onResume()
    {super.onResume();
    if(Task.getStatus() == AsyncTask.Status.PENDING){Task.execute();}

    }

    public class Updatetask extends AsyncTask<Void, Integer, Void> 
    {ProgressDialog dialog;  

    private NotificationHelper mNotificationHelper;
    private Context context;


        public Updatetask_hymns(Context context){
            this.context = context;
            mNotificationHelper = new NotificationHelper(context);
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            mNotificationHelper.createNotification();
            dialog =new ProgressDialog(DataBaseUpdateService.this);
            dialog.setTitle("Updating DB...");
            dialog.setMax(100); 
            dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            dialog.setIndeterminate(false);
            dialog.setCancelable(false);
            dialog.show();
        }

        @Override
        protected Void doInBackground(Void...unsed) {

***Code to parse xml and put in arraylist***

for(itemInsert=0;itemInsert<itemCount;itemInsert++)
{ ***insert data to database***
    if(itemCount > 0) { publishProgress((int) ((itemInsert / (float) itemCount) * 100));}
    }

            return null;}

        @Override
        protected void onPostExecute(Void unused) {
            mNotificationHelper.completed();            
            dialog.dismiss();
        }



        protected void onProgressUpdate(Integer... values) {
            mNotificationHelper.progressUpdate(values[0]);
            dialog.setProgress(values[0]);           
        }
    }

}

NotificationHelper

public class NotificationHelper {
    private Context mContext;  
    private Notification mNotification;
    private NotificationManager mNotificationManager;
    private PendingIntent mContentIntent;


    private CharSequence mContentTitle;



    private int NOTIFICATION_ID = 1;

    public NotificationHelper(Context context)
    {
        mContext = context;
    }

    /**
     * Put the notification into the status bar
     */
    public void createNotification() {
        //get the notification manager
        if(mNotificationManager==null){
        mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);}

        //create the notification
        int icon = android.R.drawable.stat_sys_download;
        CharSequence tickerText = mContext.getString(R.string.database_update_main_action); //Initial text that appears in the status bar
        long when = System.currentTimeMillis();

        mNotification = new Notification(icon, tickerText, when);

        //create the content which is shown in the notification pulldown
        mContentTitle = mContext.getString(R.string.database_update_title); //Full title of the notification in the pull down
        CharSequence contentTitle = "My notification";      
        CharSequence contentText = "0% complete"; //Text of the notification in the pull down

        Intent notificationIntent = new Intent(mContext,DataBaseUpdateService.class);
        notificationIntent.addFlags(Notification.FLAG_ONGOING_EVENT);
        mContentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0);

        //add the additional content and intent to the notification
        mNotification.setLatestEventInfo(mContext, contentTitle, contentText, mContentIntent);

        //make this notification appear in the 'Ongoing events' section
        mNotification.flags = Notification.FLAG_ONGOING_EVENT;

        //show the notification
        mNotificationManager.notify(NOTIFICATION_ID, mNotification);
    }

    /**
     * Receives progress updates from the background task and updates the status bar notification appropriately
     * @param percentageComplete
     */
    public void progressUpdate(int percentageComplete) {
        //build up the new status message
        CharSequence contentText = percentageComplete + "% complete";
        //publish it to the status bar
        mNotification.setLatestEventInfo(mContext, mContentTitle, contentText, mContentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mNotification);
    }

    /**
     * called when the background task is complete, this removes the notification from the status bar.
     * We could also use this to add a new ‘task complete’ notification
     */
    public void completed()    {
        //remove the notification from the status bar
        mNotificationManager.cancel(NOTIFICATION_ID);
    }
}
Was it helpful?

Solution

Maintain a flag to find out your activity is in background or foreground and then if it is in background show the notification and launch a pending to your activity on click of it.

if(inBackGround){
    Intent contentIntent = new Intent(this,DataBaseUpdateService.class);
    PendingIntent contentPendingIntent = PendingIntent.getActivity(
                    this, 0, contentIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

    PendingIntent.getBroadcast(this, 0, contentIntent , PendingIntent.FLAG_UPDATE_CURRENT);
    String notificationTitle ="sfdf";
    String notificationText = "dsf";
    Notification notification = new Notification(
            notificationIcon, notificationTitle, System.currentTimeMillis());
    notification.setLatestEventInfo(this, notificationTitle, notificationText, contentIntent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    // Trigger the notification.
    mNotificationManager.notify(0, notification);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top