Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 586
Parth Bhayani Avatar asked Dec 14 '25 08:12

Parth Bhayani


1 Answers

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" />
like image 83
Parth Bhayani Avatar answered Dec 16 '25 22:12

Parth Bhayani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!