2013-04-08 63 views
0

我正在使用服務在我的應用程序中運行長時間的後臺任務,在這些服務中,這些函數正在運行登錄到XMPP並從XMPP服務器獲取一些數據。我想顯示登錄完成後的進度條。如何從服務到活動獲取響應以正確更新進度欄以避免UI中出現一些異常。這樣如何獲取進度對話框,直到從android中獲取服務響應?

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

我想顯示進度條高達登錄完成了代碼,如何從服務響應登錄後完成?

+0

您的'活頁夾'的調用方法 – 2013-04-08 06:50:07

+0

@DmitryZaitsev如何?可以提供任何與此相關的示例代碼。 – 2013-04-08 06:58:47

回答

-1

您可以通過覆蓋onProgress()方法更新進度條 這裏是一個接近你的案例,你可以參考。 link

+0

onProgressUpdate()是你必須覆蓋的方法, 這個方法在AsynckTask中 – M090009 2013-04-08 07:15:22

+0

我不是在討論在asyncTask中顯示進度,它與服務有關。清楚地看問題。 – 2013-04-08 07:16:49

+0

抱歉,男人不能幫你 – M090009 2013-04-08 07:23:21

0

通過Binder您可以發送回調到您的Activity,這意味着您可以更新UI。根據

  1. Add方法您Binder(讓我們將其命名爲onProgress
  2. 從這個Binder
  3. AsyncTask調用方法爲了瞭解進度更新考慮使用觀察者模式(換句話說 - 你Activity應該聽取您的Binder的更新,或者更具體地說 - 致電Binder.onProgress方法)
+0

我不知道如何開始這個,你可以發佈任何教程。 – 2013-04-09 05:30:55

+0

@RajaReddyPolamReddy對不起,我給了你足夠的信息 - 你可以自己做。此鏈接對您有用:http://developer.android.com/guide/components/bound-services.html – 2013-04-09 06:33:23

相關問題