2017-01-04 28 views
0

私は "ABC"と呼ばれるクラスと "Notification Service"という名前のクラスを持っています。このサービスでは、作成時のアクティビティが呼び出され、 "SMS"という別のアプリケーションがあるとすぐにこのサービスを開始します。私は "SMS"アプリが "通知サービス"がバックグラウンドで実行されているかどうかをチェックし、実行していない場合は "SMS"アプリから同じサービスを開始する必要があるかどうかをチェックします。サービスの他のアプリからサービスが実行されているかどうかを確認する方法は?

コード: -

private String m_szMobileNumber; 
private String m_szEncryptedPassword; 
private String TAG = CDealsNotification.class.getSimpleName(); 

@Nullable 
@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 

@Override 
public void onCreate() { 
    super.onCreate(); 
} 

@Override 
public void onStart(Intent intent, int startId) { 
    super.onStart(intent, startId); 
    CLoginSessionManagement m_oSessionManagement = new CLoginSessionManagement(getApplicationContext());// crating object of Login Session 
    HashMap<String, String> user = m_oSessionManagement.getLoginDetails();// get String from Login Session 
    m_szMobileNumber = user.get(CLoginSessionManagement.s_szKEY_MOBILE).trim();// getting password from saved preferences.......... 
    m_szEncryptedPassword = user.get(CLoginSessionManagement.s_szKEY_PASSWORD).trim();// getting mobile num from shared preferences... 

    final Handler handler = new Handler(); 
    Timer timer = new Timer(); 
    TimerTask doServerRequest = new TimerTask() { 
     @Override 
     public void run() { 
      handler.post(new Runnable() { 
       public void run() { 
        try { 
         initialGetData(); 
        } catch (Exception e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 
      }); 
     } 
    }; 
    timer.schedule(doServerRequest, 0, 50000); //execute in every 50000 ms 
} 

private void initialGetData() { 
    try { 
     String json; 
     // 3. build jsonObject 
     final JSONObject jsonObject = new JSONObject();// making object of Jsons. 
     jsonObject.put(ServerRequestKeyStorage.s_szAGENT_CODE, m_szMobileNumber);// put mobile number 
     jsonObject.put(ServerRequestKeyStorage.s_szPASSWORD, m_szEncryptedPassword);// put password 
     jsonObject.put(ServerRequestKeyStorage.s_szRECORD_COUNT, "5");// put record count 
     jsonObject.put(ServerRequestKeyStorage.s_szLAST_COUNT, "0");// put last count 

     json = jsonObject.toString();// convert Json object to string 

     Log.e(TAG, "Server Request:-" + json); 
     final String m_DealListingURL = APIStorage.IREWARDS_URL + APIStorage.s_szDEALLISTING_URL; 
     JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, m_DealListingURL, jsonObject, new Response.Listener<JSONObject>() { 
      @Override 
      public void onResponse(JSONObject response) { 
       Log.e(TAG, "Server Response:-" + response); 
       try { 
        int nResultCodeFromServer = Integer.parseInt(response.getString(ServerResponseStorage.s_szRESULT_CODE)); 
        if (nResultCodeFromServer == CStaticVar.m_kTRANSACTION_SUCCESS) { 
         //Creating Intent for notification 
         Intent resultIntent = new Intent(getApplicationContext(), AppHomeScreenActivity.class); 

         TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext()); 
         stackBuilder.addParentStack(CRewardMain.class); 
         stackBuilder.addNextIntent(resultIntent); 
         //Initialize PendingIntent 
         PendingIntent resul = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 
         // Showing Notification 
         Notification.showNotification(getApplicationContext(), "Earn points", "Earn reward points and Get a chance to win Rs.250 on weekly contest.", resul, 100); 
        } 

       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       Log.e(TAG, "Server error:-" + error); 
      } 
     }); 
     RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); 
     requestQueue.add(jsonObjectRequest); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 

}

+0

これを試してみてくださいuは、すべての実行中のサービスを見つけることができますが、設定の下でアプリのセクションに移動することができますhttp://stackoverflow.com/questions/600207/how-to-check-if-a-service-is-running-on-android –

+0

サービスが既に実行されているかどうかをチェックせずにSMSアプリケーションからサービスを開始するだけです新しいサービスを作成しません。サービスが実行されていない場合は、新しいサービスを作成します。 –

+0

しかし、 "abc"アプリによってトリガされるサービスはどうですか? – Vishal

答えて

関連する問題