android:how to check if application is running in background

后端 未结 4 1316
Happy的楠姐
Happy的楠姐 2020-12-01 18:53

I am newbie to android. I have client server based application. Server keeps on sending the update notifications to client after every single minute and at client side my ap

4条回答
  •  执念已碎
    2020-12-01 19:56

    Only for API level 14 and above

    You can use ComponentCallbacks2 to an activity, service, etc.

    Example:

    public class MainActivity extends AppCompatActivity implements ComponentCallbacks2 {
       @Override
       public void onConfigurationChanged(final Configuration newConfig) {
    
       }
    
       @Override
       public void onLowMemory() {
    
       }
    
       @Override
       public void onTrimMemory(final int level) {
         if (level == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
            // app is in background
         }
       }
    }
    

提交回复
热议问题