Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8054385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:08:06+00:00 2026-06-05T08:08:06+00:00

Currently, I am trying to parse an online XML file and insert the data

  • 0

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);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T08:08:09+00:00Added an answer on June 5, 2026 at 8:08 am

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently trying to parse some xml data into an NSDictionary . This
ok so I am trying to parse XML data into a table, this is
I'm currently trying to parse JSON into Core Data objects. I parse the JSON
I'm new to java and servlet and currently trying to parse XML using Jericho
I'am currently trying to create and application (In c#) that parse a XML file
I'm currently trying to pull data from a database using a variable and then
I'm currently trying to parse a document into tokens with the help of regex.
Currently I am trying to parse an xml string that I already have (no
I am currently trying to read from an xml file which records the jobs
I'm currently trying to consume a list of strings from some XML using Apache

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.