How to know when I close my app? - android

My question is simple, I tried disable the bluetooth for my app when the user close the app.
I need that if the user is with the app in background, the bluetooth maintain active, but if the user slide the app out of screen, the bluetooth closes.
I know how disable the bluetooth.
if (mBluetoothAdapter.isEnabled()){
mBluetoothAdapter.disable();
}
Works perfectly, but I need call that 3 lines when the user close the app, I tried with onDestroy, onPause, onStop, but noone works for me.
onDestroy method is called when the user touch back button and the task go for background.
onPause method is called when the user touch the recents button, but the app is not closed.
onStop method same of onPause.
So... How to know when the user close the app?
Thank you

You can write a method to check if the app is running in background or not.
private boolean isAppIsInBackground(Context context) {
isInBackground = true;
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
for (String activeProcess : processInfo.pkgList) {
if (activeProcess.equals(context.getPackageName())) {
isInBackground = false;
}
}
}
}
} else {
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
if (componentInfo.getPackageName().equals(context.getPackageName())) {
isInBackground = false;
}
}
return isInBackground;
}
This method returns true if the app is running in background.Then you can enable or disable bluetooth accordingly.
Also you can set a flag in your onResume and onPause methods of the BaseActivity which is extended by all other activitues so that you can check the flags to know whether app is running or not.

Make a BaseActivity and override its OnPause() method and extend
every Activity from BaseActivity .
I would also like to mention that onPause() is fired even when switching between multiple activities, so you'd need to track when it's pausing to go to another screen.
Reference : http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

If you want Bluetooth disabled when the user exits your activity, disable it in onBackPressed().

Related

android, how to tell whether the app is in killed state by packagename

Having an android app which has service running to listen to the FCM notification.
By app in killed state I mean when swipe off the app from the recent activist app list, or close the app by tapping on the home button, or backpress on the app until the app closes (after all activities are popped out from backstack), or for any reason the OS killed the app.
There are functions could be used with the app's packagename to get some app's state info.
this one can help to tell the app is in background, but may not be killed.
public class ArchLifecycleApp extends Application implements LifecycleObserver {
#Override
public void onCreate() {
super.onCreate();
ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
}
#OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onStop() {
//App in background
}
#OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStart() {
// App in foreground
}
}
this one can tell app is in FG only:
boolean isAppInFG(Context appContext, String packageName) {
boolean appInFG = false;
ActivityManager activityManager = (ActivityManager) appContext.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses != null) {
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND &&
appProcess.processName.equals(packageName)) {
appInFG = true;
break;
}
}
}
return appInFG;
}
after pressed home button, or swipe out the app from the recent application list, the appProcess.importance is always 300 and is same as if the app is in background (covered by other app).
Question: in this case is there way to tell the app is killed (not just simply in background)?
In a comment you wrote:
I want to determine: should the app go through a fresh re-launch (app
is killed) or just bring the app to front?
If you use a "launch Intent", this will handle all of this for you. If the app is already running, it will just bring the app to the foreground in whatever state it was in. If the app is not running, it will launch the app fresh.
To get a "launch Intent", you can use PackageManager.getLaunchIntentForPackage()

How to execute some event if RN has been killed?

Not sure if I understand correctly but according to this link
Deliver silent notifications and wake up your app in the background on the user's device.
It sounded to me that it's possible to perform some action even if the app has been killed.
Currently I'm using OneSignal as below:
OneSignal.addEventListener('received', this.onReceived);
onReceived(store, notification) {
store.dispatch(receivedNotification({ notification }));
}
However the above will only be able to dispatch action if the app is in background or foreground, but once the app been killed, despite receiving notification successfully, onReceived event will not be fired.
So my question is whether is it possible to "wake" my RN app in the background and dispatch a redux action?
There is no event that you can catch before app is killed. You also cannot prevent the user from killing your app. And once it is killed you cannot 'wake' it up, as your code is not running. You can only do such tasks when the app is in the background/foreground.
Yes, it is possible you can execute some event if RN has been killed.
now the question is how basically I am also using react-native and I have got some challenges also, I also want to execute some event if RN has been killed, but I did not get any answer actually my concern is to open an app which can and show a call screen when calling notification is received so I dig a lot and I found a solution,
first I created a bridge connection between javascript to java and then write a wakeful service which gets called every time when we receive notification and then I call my background intent service and in this service I wake up my activity and set some flags which help me to open screen when screen is lock depends on the conditions
// receiver Service //
public class MessagingService extends WakefulBroadcastReceiver {
private static final String TAG = "FirebaseService";
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, String.valueOf(!isAppOnForeground(context)));
if (intent.getExtras() != null) {
if (!isAppOnForeground((context))) {
//This get called every time you receive notification
}
}
}
private boolean isAppOnForeground(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses == null) {
return false;
}
final String packageName = context.getPackageName();
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {
return true;
}
}
return false;
}
}

Check application is minimized/background

I am so surprised!
Because not able identify application currently in foreground or background,
when i pressed home button and my application is not in foreground. then after i have used to check application running in foreground or not.
public boolean isAppOnForeground()
{
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses == null) { return false; }
final String packageName = context.getPackageName();
for (RunningAppProcessInfo appProcess : appProcesses)
{
if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) { return true; }
}
return false;
}
it will returns always true even if my application actually not in foreground.
Note:
My application have 2 services,
1 service will be canceled when application is removed from recent task list( that i have achieved.)
2 service will be canceled when application goes in background. (that is check wifi signal strength.)
how to know app is foreground or background?
how to stop service when application is not in foreground?
Rely on Activity callback methods, such as onPause(), onStop() and onResume(), that will give you hints on the current state of Activity. The Activity reference gives a very broad explanation of every of those methods.
try handling onpause() event of activity class. Onpause is called when activity goes out of focus.

How to detect whether the app is coming from background? [duplicate]

I want to detect and count when Activity goes from background to foreground (when activity is visible, increase count).I tried to use flag in onPause() and onResume() like this:
void onPause(){
flag = true;
}
void onResume(){
if(flag){
//save to shared reference.
saveCount(getCount(count) + 1);
flag = false;
}
}
It works when user press home key and relaunches the app, but when I transfer Activity then goes back, it still increases the count, because it calls onPause(). How to prevent that? Or is there anyway to count this?
Use this method to check wheter app is brought to background:
private boolean isApplicationBroughtToBackground(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
return true;
}
}
return false;
}
It requires the GET_TASKS permission:
<uses-permission android:name="android.permission.GET_TASKS" />

How I can detect that my Android app on the foreground?

I'm trying to know my or no application on the foreground using the next code from BroadcastReceiver:
boolean inForeground = false;
ActivityManager actMngr = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> runningAppProcesses = actMngr.getRunningAppProcesses();
for (RunningAppProcessInfo pi : runningAppProcesses) {
if (context.getPackageName().equals(pi.processName)) {
inForeground = pi.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
}
}
But this code always return true, even when my application in background.
I found next solution:
if (actMngr.getRunningTasks(1).get(0).topActivity.getPackageName().equals(context.getPackageName())){
Log.d(TAG, "My");
} else {
Log.d(TAG, "Not my");
}
Is this code correct or not?
Or maybe somebody know more simple variant?
Thanks!
The most direct way is to track your foreground status using onResume() and onPause(). Refer to the Activity lifecycle model.

Categories

Resources