0

I am using service for running long background tasks in my application, in the service these functions are running login to XMPP and getting some data from XMPP server. i want to show the progress bar upto login completed. How to get response from service to activity to Update progress bar properly to avoid some exceptions in UI. I am calling service like this

final Intent gtalk_intent = new Intent(AccountsActivity.this, GtalkService.class);             
gtalk_intent.putExtra("user_name", acc.getAcc_Name());
gtalk_intent.putExtra("user_pass", acc.getAcc_Pass());               
startService(gtalk_intent);

this is the code from service

public class PmService extends Service {

   @Override
   public IBinder onBind(Intent intent) {               
      return mBinder;
   }

   public class PmBinder extends Binder {
      public PmService getService() {
         return PmService.this;
      }
   }

   @Override
   public void onCreate() {

      super.onCreate(); 
      context = this;       
      app_preferences = new AppPreferences(this);

      chat_source = new ChatsDataSource(this);  
      chat_source.open();
   } 

   @Override
   public int onStartCommand(Intent intent, int flags, int startId) {
      Bundle extras = intent.getExtras();       
      if(extras == null)  {         
         full_name = extras.getString("user_name");
         if(full_name.contains("@")) {
            String[] _na = full_name.split("@");
            U_name = _na[0];
        }
        U_pass = extras.getString("user_pass");                    
    }       
    new PmAsync().execute();
    return START_STICKY;
}

private class PmAsync extends AsyncTask<Void, Void, Void> {

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

    @Override
    protected void onPostExecute(Void result) {          
        super.onPostExecute(result);
    }

    @Override
    protected Void doInBackground(Void... params) {     
        SASLAuthentication.supportSASLMechanism("PLAIN", 0);
        ConnectionConfiguration config = new ConnectionConfiguration(server_host, SERVER_PORT, SERVICE_NAME);
        configure(ProviderManager.getInstance());
        m_connection = new XMPPConnection(config);
        try {            
            m_connection.connect();                  
            Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);              
        } catch (XMPPException e) {             
            e.printStackTrace();
        }           
        try {               
            m_connection.login(U_name, U_pass);
            setPacketFilters();                     
        } catch (XMPPException e) {                 
        }

        return null;
    }       
}

i want to show the progress bar upto login completed, how to response from service after login completed?

Mr.Sandy
  • 4,299
  • 3
  • 31
  • 54
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166

2 Answers2

0

Via Binder you can send callbacks to your Activity, which means that you can update UI.

  1. Add according method to your Binder (let's name it onProgress)
  2. From your AsyncTask call method of this Binder
  3. In order to know about progress updates consider using Observer pattern (in other words - your Activity should listen for updates of your Binder, or more specifically - of calling Binder.onProgress method)
Dmitry Zaytsev
  • 23,650
  • 14
  • 92
  • 146
-1

You can update the progress bar via overriding the onProgress() method here is a close to your case that you can refer to.link

Community
  • 1
  • 1
M090009
  • 1,129
  • 11
  • 20