0

I have an app called "ABC" and a class named as "Notification Service" in which i start this service immediately when its activity on create called and i have another app called "SMS". what i want "SMS" app will check whether "Notification Service" is running in background or not if not running then i have to start same service from "SMS" app.Please let me know.

code for service:-

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

}

Vishal
  • 67
  • 2
  • 8
  • try this http://stackoverflow.com/questions/600207/how-to-check-if-a-service-is-running-on-android – D.J Jan 04 '17 at 05:33
  • Just start the service from the sms app without checking, if the service is already running it won't create a new service, if service is not running it will create a new service. – Akshay Bhat 'AB' Jan 04 '17 at 05:51
  • but what about that service which is triggered by "abc" app – Vishal Jan 04 '17 at 05:52
  • Check @Commonsware answer [here](http://stackoverflow.com/questions/3692915/create-only-one-instance-of-service-android) – Akshay Bhat 'AB' Jan 04 '17 at 05:59

1 Answers1

-1

Good solution is here https://stackoverflow.com/a/5921190/4853552 or u can go to the app section under the setting there u can find all the running services

Community
  • 1
  • 1
Shubham Jain
  • 2,365
  • 2
  • 17
  • 29