Press home button to stop service

后端 未结 3 382
谎友^
谎友^ 2021-01-16 12:08

i want to if the user press home button my service will be stop , this is my code but it\'s not working for me, please help me:

@Override
public boolean onKe         


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-16 12:47

    Make a root activity and add this,

    @Override
        public void onTrimMemory(int level) {
            super.onTrimMemory(level);
            if (level == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
                // Get called every-time when application went to background.
                System.exit(0);
            }
    
        }
    

    extend each activity with rootActivity, it will shutdown service on home/menu button press

提交回复
热议问题