title: How to prevent Activity automatically open in android?
body: <p>I am developing an app that contains an IntentService to send a request to a server when the app is in background and the response from server will start the activity using an intent. Problem is that it opens the activity automatically without any user interaction when the app is in background. How to avoid this only open activity when app comes to foreground.</p>
  
  <p>Here is my IntentService code:</p>
  
  <pre><code>public class OTPIntentService extends IntentService {
      /**
       * Creates an IntentService.  Invoked by your subclass's constructor.
       *
       * @param name Used to name the worker thread, important only for debugging.
       */
      public CRegistrationSessionManagement m_oSessionManagement;
      public String m_szMobileNumber, m_szEncryptedPassword;
      public String TAG = "OTPIntentService";
  
      public OTPIntentService() {
          super("OTPIntentService");
      }
  
      @Override
      protected void onHandleIntent(Intent intent) {
          final Context m_Context = getApplicationContext();// get activity context
          m_oSessionManagement = new CRegistrationSessionManagement(m_Context);// creating object of Registartion session
          // retreive user data from shared preferencce........
          HashMap&lt;String, String&gt; user = m_oSessionManagement.getRegistrationDetails();// getting String from Regisatrtion session
          m_szEncryptedPassword = user.get(CRegistrationSessionManagement.s_szKEY_PASSWORD).trim();// get password from registartion session
          m_szMobileNumber = user.get(CRegistrationSessionManagement.s_szKEY_MOBILENUMBER).trim();// get mobile number from registartion session
  
          try {
              String json;
              JSONObject jsonObject = new JSONObject();
              jsonObject.put("agentCode", m_szMobileNumber);
              jsonObject.put("pin", m_szEncryptedPassword);
              jsonObject.put("otpCode", COTPVerificationDataStorage.getInstance().getM_szOtp());
  
              json = jsonObject.toString();
              System.out.println("Request:-" + json);
              JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, CServerAPI.m_szOtpverification, jsonObject, new Response.Listener&lt;JSONObject&gt;() {
                  @Override
                  public void onResponse(JSONObject response) {
                      System.out.println("Response:-" + response);
                      try {
                          if (response.getString("resultDesc").equalsIgnoreCase("Transaction Successful")) {
  
                          } else if (response.getString("resultDesc").equalsIgnoreCase("OTP MisMatch")) {
                              if (NetworkUtil.isAppIsInBackground(m_Context)) {
  
                              } else {
                                  Intent i = new Intent(m_Context, COtpManualVerificationScreen.class);
                                  i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                  startActivity(i);
                              }
  
                          } else if (response.getString("resultDesc").equalsIgnoreCase("otpCode Can Not Be Empty")) {
                              if (NetworkUtil.isAppIsInBackground(m_Context)) {
  
                              } else {
                                  Intent i = new Intent(m_Context, COtpManualVerificationScreen.class);
                                  i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                  startActivity(i);
                              }
  
                          }
  
                      } catch (JSONException e) {
                          e.printStackTrace();
                      }
                  }
              }, new Response.ErrorListener() {
                  @Override
                  public void onErrorResponse(VolleyError error) {
                      System.out.println("Error:-" + error);
                  }
              });
              RequestQueue requestQueue = Volley.newRequestQueue(m_Context);
              requestQueue.add(jsonObjectRequest);
          } catch (JSONException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>