Search code examples
androidchatbackground-foregroundforeground-service

Foreground method is not working in Jellybean in Android


I have implemented the service for my chat application and the concept should be online and last seen just like whatsapp.

When I implemented online and last seen service to my app and when I checked from lollipop to lollipop it is working 100% fine but when I checked the same thing from jellybean to lollipop it is not working any more.

@Override
    protected void onStop() {
        super.onStop();

        if (Utils.isAppSentToBackground(getApplicationContext())) {

            // call web Service here
            // Log.e("Angel", "in back");
            Utils.setInBackGround(getApplicationContext(), true);
            callOnlineOfflineService("0");
        }
    }


// code for resume called
    @Override
    protected void onResume() {
        super.onResume();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
        if (Utils.isInBackGround(getApplicationContext())) {
            // Log.e("Angel", "From back");

            Utils.setInBackGround(getApplicationContext(), false);
            callOnlineOfflineService("1");
        }
    }

Here, Util is my common class in which I have implemented common method and the setInBackGround method set the credentials to the SessionManager.

void callOnlineOfflineService(String status) {

        Log.e("status:::::online", "status" +status);

        if(!myclass.isConnected(getApplicationContext())){
            errordialog(getResources().getString(R.string.no_internet));
            return;
        }

        final List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("mode", "setUserOnlineStatus"));
        params.add(new BasicNameValuePair("userId", uid));
        params.add(new BasicNameValuePair("status", status));

        Log.e("status:::::online", "status" +params);

            Runnable runnable = new Runnable() {
                public void run() { 

                    UserFunctions userFunction = new UserFunctions(getApplicationContext());
                    JSONObject json = userFunction.CommonObject(params);

                        try {

                            final String s = json.getString("status");
                            Log.e("status:::::online", "status" +s);

                        } catch (JSONException e) {
                            e.printStackTrace();
                             return;
                        }
                }
            };
            Thread mythread = new Thread(runnable);
            mythread.start();
    }

Your hard work and efforts will be highly appreciated and thanks in advance.


Solution

  • I have tried and came to the conclusion for this and it works.

    Just make sure, you have given the below permission, after that the above code is working fine for anyone otherwise it is not working for some OS.

    <uses-permission android:name="android.permission.GET_TASKS" />