Gajanand Swamy
Gajanand Swamy

Reputation: 2108

How to check device idle for some minutes in Application class?

I want to check the device idle state for some minutes. i am able to get when it is in particular activity. like below

 public class MainActivity extends AppCompatActivity {

    Handler handler;
    Runnable r;
    private ContentResolver contentResolver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);




        handler = new Handler();
        r = new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, "user is inactive from last 10 seconds", Toast.LENGTH_SHORT).show();
            }
        };
        startHandler();


    }

    @Override
    public void onUserInteraction() {
        // TODO Auto-generated method stub
        super.onUserInteraction();
        stopHandler();//stop first and then start
        startHandler();
    }

    public void stopHandler() {
        handler.removeCallbacks(r);
    }

    public void startHandler() {
        handler.postDelayed(r,  10000); //for 10 seconds
    }


}

but i want to check device idle state for entire application.i checked in application class there is no method as onUserInteraction() is there any other trick for this.

Upvotes: 0

Views: 117

Answers (2)

Jaspreet Kaur
Jaspreet Kaur

Reputation: 1720

Create 3 Methods in application class, and Reset the value of activityVisible variable inside that method

public static boolean activityVisible; // Variable that will check the current activity state       

public static boolean isActivityVisible() {
        return activityVisible; // return true or false
    }

public static void activityResumed() {
        activityVisible = true;// this will set true when activity resumed

    }

public static void activityPaused() {
        activityVisible = false;// this will set false when activity paused

}

You can call these method by following way

AppClass.activityResumed();// On Resume notify the Application
AppClass.activityPaused();// On Pause notify the Application

Perform Action when activity is visible

boolean isVisible = AppClass.isActivityVisible();

Upvotes: 0

Manish Yadav
Manish Yadav

Reputation: 27

I think you should Create a BaseActivity which will extend AppCompatActivity and put your code inside BaseActivity and replace it in all Activities with AppCompatActivity. I hope it will help you.

Upvotes: 1

Related Questions