Android app discovery on background

I need to turn off Bluetooth when my app “fades into the background” / “becomes inactive”.

I tried to do this in onPause () of my MainActivity, but it didn’t work, since BT disconnects (onPause () for the main activity), even when I start a new action showing the object detail of the selected item from Mainactivity.

I need some kind of "onPause ()" of my application from more than one action.

I think nothing of the kind exists, is there a preferred solution?

+6
source share
6 answers

UPDATE : Beware, according to the comments, this does NOT work in all cases !!!

The solutions listed here on SO seem to me too complicated. I came up with this:

Create a static variable accessible from all your Activities to determine the number of currently running.

public class SharedData { static int runningActivities = 0; } 

In each action, override two methods.

 public void onStart() { super.onStart(); if (SharedData.runningActivities == 0) { // app enters foreground } SharedData.runningActivities++; } public void onStop() { super.onStop(); SharedData.runningActivities--; if (SharedData.runningActivities == 0) { // app goes to background } } 

You can create an active MyActivity, put this code in it, and make all your actions extended for this class.

+8
source

You can refer to this question. How to determine when the Android application goes into the background and comes to the foreground. The guy who answered this decided to use onFocuschanged don I don’t know how effective this method is, anyway continue to research on google

 private boolean isApplicationBroughtToBackground() { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List tasks = am.getRunningTasks(1); if (!tasks.isEmpty()) { ComponentName topActivity = tasks.get(0).topActivity; if (!topActivity.getPackageName().equals(context.getPackageName())) { return true; } } return false; } 
+2
source

For more than one Activity application, I would recommend that each action exercise time-based visibility controls. Run each onPause () status check timer and also cancel this timer on every onResume () method.

Here is an example

  @Override protected void onPause() { TimerTask backgroundCheck = new TimerTask() { @Override public void run() { ActivityManager am = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> tasks = am.getRunningTasks(1); if (!tasks.isEmpty()) { ComponentName topActivity = tasks.get(0).topActivity; if (!topActivity.getPackageName().equals(getApplicationContext().getPackageName())) { // APP in background, do something } } // APP in foreground, do something else } }; Timer isBackgroundChecker = new Timer(); isBackgroundChecker.schedule(backgroundCheck, 1000, 1000); super.onPause(); } 

After doing this, remember to cancel Timer isBackgroundChecker = new Timer();

+2
source

RunningTaskInfo not recommended because its "method is only for debugging and presenting task management user interfaces." well, if you are developing an application for ICS and above, I found this link very useful!

http://vardhan-justlikethat.blogspot.in/2013/05/android-solution-to-detect-when-android.html

+2
source

To find that the application goes into the background, override the following method in the application class of your application:

 @Override public void onTrimMemory(int level) { super.onTrimMemory(level); if(level == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) //called when app goes to background } 
+2
source

You can implement onPause () for each of your actions, but I don’t think you need to turn off bluetooth yourself, some users can use Bluetooth for some other reason, or they want to turn it off themselves (what am I doing)

+1
source

All Articles