2014-02-22 45 views
0

什麼是應對以下情況的最好辦法:我有一個IntentService這確實與服務器同步(這是通過一個活動來到前臺,或GCM消息觸發 ,所以偶然的onoy)。有時需要作爲結果的用戶操作,並且給定的命令/請求是響應XML的一部分。啓動沒有上下文的意圖

基本上有兩種選擇,它可以是一個是/否的問題,或完整的活動來例如選擇所需的語言。

我該怎麼做,或者什麼是最好的方法是什麼?如果我嘗試使用IntentService的上下文啓動Activity,則不會發生任何事情。我可以寫一個抽象的活動,這是我在我所有的活動延伸,其中發送那些接收並隨後啓動活動形成活躍的活動,但不知道這是否是做它在Android的最好的方式廣播消息。

任何建議,將不勝感激!

[編輯:作爲建議的一些代碼]

public class SyncService extends IntentService{ 

    public SyncService(){ 
     super("SyncService"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
      iDomsAndroidApp app = ((iDomsAndroidApp) getApplicationContext()); 
      DataManager manager = app.getDataManager(); 
      manager.updateData(this); 
    } 
} 


public class DataManager { 

    // For brevity, this is called with the DOM.Document with the actions to be preformed 
    private void checkForActions(Document doc, SyncUpdateInterface syncInterface){ 
     NodeList objects = null; 
     NodeList rootNodes = doc.getElementsByTagName("actions"); 

     for (int j = 0; j < rootNodes.getLength(); j++) { 
      Element rootElement = (Element) rootNodes.item(j); 
      if (!rootElement.getParentNode().getNodeName().equals("iDoms")) { 
       continue; 
      } 
      objects = ((Element) rootNodes.item(j)).getElementsByTagName("action"); 
      break; 
     } 

     if(objects == null || objects.getLength() == 0){ 
      Log.d(iDomsAndroidApp.TAG, "No actions"); 
      return; 
     } 


     for (int j = 0; j < objects.getLength(); j++) { 
      Element element = (Element) objects.item(j); 
      String action = ((Element) element.getElementsByTagName("command").item(0)).getTextContent(); 
      if(action == null) return; 
      Log.d(iDomsAndroidApp.TAG, "Action: " + action); 
      try{ 
       if(action.equalsIgnoreCase("selectLanguage")){ 
        if(syncInterface == null || syncInterface.getContext() == null) throw new Exception("No context, so cannot perform action"); 
        iDomsAndroidApp app = ((iDomsAndroidApp) iDomsAndroidApp.getAppContext()); 
        // The app.actionIntent is just a central function to pick the right intent for an action. 
        syncInterface.getContext().startActivity(app.actionIntent("settings", iDomsAndroidApp.context)); 
       } else if (action.equalsIgnoreCase("markAllAsRead")) { 
        if(syncInterface == null | syncInterface.getContext() == null) throw new Exception("No context, so cannot perform action"); 
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(syncInterface.getContext()); 
        alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int id) { 
          // User clicked OK, so save the result somewhere 
          // or return them to the component that opened the dialog 
          iDomsAndroidApp app = ((iDomsAndroidApp) iDomsAndroidApp.getAppContext()); 
          app.getDataManager().markAllAsRead(null); 
         } 
        }); 
        alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int id) { 

         } 
        }); 

        alertDialogBuilder.setTitle(iDomsAndroidApp.context.getString(R.string.markAllAsRead)); 
        alertDialogBuilder.setMessage(iDomsAndroidApp.context.getString(R.string.markAllAsReadText)); 

        alertDialogBuilder.show(); 
       } 
      } catch (Exception e){ 
       Log.w(iDomsAndroidApp.TAG, "Problem performing the action " + element.getTextContent(), e); 
       sentCrashReport("Problem performing the action " + element.getTextContent(), e); 
      } 
     } 
} 

我嘗試使用我的SyncInterface,因爲它給了IntentService的背景下,但認爲這是一個笨拙,但並不起作用:

public interface SyncUpdateInterface { 
    public void doProgress(String message, int increment, int total); 
    public void doProgress(String message, int increment); 
    public void doProgress(String message); 
    public Context getContext(); 
} 
+0

一些代碼,將有助於:) – AndyFaizan

+0

我加了一些重要的代碼片段 –

回答

0

您可能必須重新考慮你的方法。 intentservice只在onHandleIntent()方法的持續時間內存在。也就是說,一旦達到了onHandleIntent()方法的最後一行代碼,IntentService就會自行停止。

+0

我會在廣播意圖的建議是一個很好的辦法的辦法,然後在這種情況下,因爲它會分開行動的形式IntentService –

+0

似乎有點迂迴的。據我所知,你正試圖保持與DataManager類的服務器的連接,但你偶爾需要一次用戶輸入。我認爲擴展Service而不是IntentService會更有意義(因此您可以控制服務何時結束)。你需要考慮你的網絡活動創建工作線程,並使用一個處理程序從服務中創建UI對象,並與該工作線程 – rperryng

+0

溝通,我應該有指定的意圖服務僅在用戶操作啓動(例如,該應用程序正在啓動/前景)和GCM消息。所以這只是偶爾的。但是如果我願意做你的建議,我將如何保持工作者和用戶界面之間的溝通清晰,尤其是在可能處於前臺的多項活動中。 –