【问题标题】:How to get progress dialog until get the response from service in android?如何获得进度对话框,直到从android中的服务获得响应?
【发布时间】:2013-04-08 06:47:58
【问题描述】:

我正在使用服务在我的应用程序中运行长时间的后台任务,在服务中,这些功能正在运行登录到 XMPP 并从 XMPP 服务器获取一些数据。我想显示进度条直到登录完成。如何从服务获取响应以正确更新进度条以避免 UI 中的一些异常。 我正在调用这样的服务

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);

这是来自服务的代码

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;
    }       
}

我想在登录完成之前显示进度条,登录完成后如何从服务响应?

【问题讨论】:

  • 你的Binder的调用方法
  • @DmitryZaitsev 怎么样?你能提供任何相关的示例代码吗?

标签: android service xmpp progressdialog asmack


【解决方案1】:

您可以通过Binder 向您的Activity 发送回调,这意味着您可以更新UI。

  1. 根据您的Binder添加方法(我们将其命名为onProgress
  2. 从你的AsyncTask调用这个Binder的方法
  3. 为了了解进度更新,请考虑使用观察者模式(换句话说 - 你的 Activity 应该监听你的 Binder 的更新,或者更具体地说 - 调用 Binder.onProgress 方法)

【讨论】:

【解决方案2】:

您可以通过覆盖 onProgress() 方法来更新进度条 这是一个接近你的案例,你可以参考。link

【讨论】:

  • onProgressUpdate() 是你必须重写的,这个方法在 AsynckTask 中
  • 我不是在说异步任务中显示的进度,它与服务有关。看清楚问题。
猜你喜欢
  • 2019-08-24
  • 2019-09-11
  • 1970-01-01
  • 1970-01-01
  • 2015-07-24
  • 1970-01-01
  • 1970-01-01
  • 2014-04-24
  • 2015-11-14
相关资源
最近更新 更多