title: Launching an intent without context
body: <p>What is the best way to deal with the following situation:
  I have a IntentService which does synchronisation with the server (this is triggered by either an Activity coming to the foreground, or a GCM message, so onoy occasional). Sometimes there is a user action needed as a result, and the given command/request is part of the response XML.</p>
  
  <p>There are basically two options, it is either a yes/no question, or a full Activity to for example select the desired language.</p>
  
  <p>How can I do this, or what would be the best way? If I try to launch the Activity with the context of the IntentService nothing happens. I could write a abstract Activity, which I extends in all my Activities and sent a broadcast message which those receive and subsequent start the Activity form the activity which is active, but don't know if that is the best way to do it in Android.</p>
  
  <p>Any suggestions would be appreciated!</p>
  
  <p>[EDIT: as suggested some code]</p>
  
  <pre><code>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 &lt; 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 &lt; 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);
              }
          }
  }
  </code></pre>
  
  <p>I tried using the my SyncInterface, as it gives the context of the IntentService, but think it is a but clumsy and doesn't work:</p>
  
  <pre><code>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();
  }
  </code></pre>