Foreground method not working in Jellybean in Android

I have implemented a service for my chat app and the concept needs to be online and last visible just like whatsapp.

When I implemented an online app and last seen service for my app, and when I checked from lollipop to lollipop, it works 100% fine, but when I tested the same from jellybean to lollipop, it doesn't work anymore.

@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 generic class where I implemented the generic method and setInBackGround method set credentials for 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 thankful in advance.

+3


source to share


1 answer


I tried and concluded for this and it works.

Just make sure you give below permission, after which the above code works fine for everyone, otherwise it doesn't work for any OS.



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

      

+2


source







All Articles