Search code examples
androidandroid-alertdialogbackground-service

Android Background issues when user is in active state


I'm working on app which is required user in active feature like if user is not available on the application more than 15 min. it shows some popup on the last activity we used, when we click okay it redirects to login screen.

It is working absolutely fine when i opened back my app exactly after 15 minutes to around 30 minutes .

My problem is now, when i open my app after 45 min or more than 1 hour, it doesn't work, it doesn't show in activity popup. it just opened the last activity i used.

I tried with below code added in splash activity:

if (!isTaskRoot()
               && getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
               && getIntent().getAction() != null
               && getIntent().getAction().equals(Intent.ACTION_MAIN)) {

           finish();
           return;
       }

Here is my BaseActivity class used for in active state checking

public class MyBaseActivity extends AppCompatActivity {

    AlertDialog alertDialog;

    Context context;

    public static final long DISCONNECT_TIMEOUT = 900000; // 15 min = 15 * 60 * 1000 ms

    private  Handler disconnectHandler = new Handler(){
        public void handleMessage(Message msg) {
        }
    };

    private Runnable disconnectCallback = new Runnable() {
        @Override
        public void run() {

            LayoutInflater li = LayoutInflater.from(MyBaseActivity.this);
            View promptsView = li.inflate(R.layout.acount_status_dialogue, null);
            final TextView userInput = (TextView) promptsView.findViewById(R.id.txtTitle);
            final TextView userInput1 = (TextView) promptsView.findViewById(R.id.txtTitle1);

            userInput1.setText("USER IN-ACTIVE");
            userInput.setText("Due to user is inactive from last 15 minutes. Please Login Again");

            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MyBaseActivity.this,R.style.DialogLevelsStyle);
            // set prompts.xml to alertdialog builder
            alertDialogBuilder.setView(promptsView);
            // set dialog message
            alertDialogBuilder
                    .setCancelable(false)
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                            //do things
                            Intent i = new Intent(MyBaseActivity.this, SignInActivity.class);
                            //i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(i);
                            finish();
                            Constant.val = 1;
                            AccountUtils.setValue("1");
                        }
                    });

            // create alert dialog
            alertDialog = alertDialogBuilder.create();
            // show it
            alertDialog.show();

            // Perform any required operation on disconnect
        }
    };

    public void resetDisconnectTimer(){
        Log.i("Main", "Invoking logout timer");
        //disconnectHandler.removeCallbacks(disconnectCallback);
        disconnectHandler.postDelayed(disconnectCallback, DISCONNECT_TIMEOUT);
    }

    public void stopDisconnectTimer(){
        Log.i("Main", "cancel timer");
        disconnectHandler.removeCallbacks(disconnectCallback);
    }

    @Override
    public void onUserInteraction(){
        resetDisconnectTimer();
    }

    @Override
    public void onResume() {
        super.onResume();
        resetDisconnectTimer();
    }

    @Override
    public void onStop() {
        if (Constant.isAppIsInBackground(this)) {
            stopDisconnectTimer();
            resetDisconnectTimer();
        }else {
            stopDisconnectTimer();
        }
        super.onStop();
        //stopDisconnectTimer();
    }
}

Please find out my issue. thanks in advance.


Solution

  • Save the current time when the user put your app to background (for example in SharedPreferences), and when the user starts again your app calculate the diff and show what you want on the screen.