如何通过其他应用检查服务是否正在运行?

时间:2017-01-04 05:30:39

标签: android

我有一个名为“ABC”的应用程序和一个名为“Notification Service”的类,我在其中创建活动时立即启动此服务,并且我有另一个名为“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();
    }
}

}

1 个答案:

答案 0 :(得分:0)

好的解决方案在这里https://stackoverflow.com/a/5921190/4853552或者您可以在设置下的应用部分找到所有正在运行的服务