I want to get the foreground activity running. And I am able to get this information by using activity manager by using following code.
activityManager = (ActivityManager) FWCommon.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
runningTaskInfoList = activityManager.getRunningTasks(1);
componentName = runningTaskInfoList.get(0).topActivity;
Suppose I have two activities A and B and i call this above code from onResume() of both activities A and B.
Following is the problem
When activity A starts above code gives me topActivity = A
Then I move from activity A to B and above code gives me topActivity = B
Then i press Back to move back from activity B to A and above code again gives me topActivity = B instead of A.
Thx
Dalvin
Try using getRunningAppProcesses() to get a list of RunningAppProcessInfo. Then go through each RunningAppProcessInfo and check if it is in the foreground by doing this:
List<ActivityManager.RunningAppProcessInfo> processes = manager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo process : processes)
{
// Take a look at the IMPORTANCE_VISIBLE property as well in the link provided at the bottom
if (process.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND)
{
// Put code here
}
}
Click here and here for more information.
Related
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().
I wand a Broadcast receiver invoke when another applications a particular activity is opened in android. For example When Navigation page in Map application that installed in android phone, When opened and showing the Navigation, I want to show a Toast that display "Currently showing Navigation". So how can I achieve that?
This below code will help you to identify windo change event
public class AccessibilityEventService
extends AccessibilityService {
#Override
public void onAccessibilityEvent(AccessibilityEvent event) {
if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED)
Log.i("op",event.getPackageName());
}
}
but it has to enabled by users from Accessibility under settings.
or You can check current top activity in a time gap using following code
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
// get the info from the currently running task
List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);
Log.d("topActivity", "CURRENT Activity ::"
+ taskInfo.get(0).topActivity.getClassName());
ComponentName componentInfo = taskInfo.get(0).topActivity;
componentInfo.getPackageName();
As my understood that you need to show a toast on onRecieve Fuunction
Toast.makeText(context,"your text",2000).show();
//context is input parameter from onRecieve function
In my app i want to check whethere any other app launches or not from my app.So i am currently usimg Activitymanager to find the top activity like this
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);
Log.d("topActivity", "CURRENT Activity ::"
+ taskInfo.get(0).topActivity.getClassName());
ComponentName componentInfo = taskInfo.get(0).topActivity;
componentInfo.getPackageName();
But i want to run this code in background service.So that i can detect When other app launches through this.So is there any idea to do this.Plz help me.thanks
By using service you can achieve this..
public void checkActivity() {
handler = new Handler();
activityRunnable = new ActivityRunnable();
handler.postDelayed(activityRunnable, 500);
}
private class ActivityRunnable implements Runnable {
#Override
public void run() {
ActivityManager manager = (ActivityManager) getApplicationContext()
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> runningTasks = manager.getRunningTasks(1);
if (runningTasks != null && runningTasks.size() > 0) {
ComponentName topActivity = runningTasks.get(0).topActivity;
// Here you can get the TopActivity for every 500ms
if(!topActivity.getPackageName().equals(getPackageName())){
// Other Application is opened
}
handler.postDelayed(this, 500);
}
}
}
Call the checkActivity() in onCreate of service and dont forgot to remove the handler.removeCallbacks(activityRunnable); callbacks in onDestroy
start the service in LauncherActivity onCreate and stop the service in LauncherActivity onDestroy
Note: Dont forgot to add the permission in your manifest
<uses-permission android:name="android.permission.GET_TASKS" />
You should use Service for this. Then only you can achieve the background process..Please Go through about Services http://developer.android.com/guide/components/services.html and you can see this also http://www.vogella.com/articles/AndroidServices/article.html both URLs will help you
instead of service Use receiver to receive the launch event
and there you can implement this code.
The approach you are taking is wrong and prone to have your app rejected from the Google Play Store (plus it may stop working).
Check this thread to see possible ways to detect wether your app went to the background or not.
I want to detect when the app comes from back ground to foreground every time.
I'm geting the state using the following code
topActivity.getPackageName().equals(context.getPackageName())
But I need to execute the code repeatedly so that I could detect every time when my app comes to foreground
It's your app. Activity always knows if it is visible or not. So make every your activity tell someone it's state. You don't need to poll app state. Check this answer.
Try this :
public static String isApplicationSentToBackground(final 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";
}
Hope this helps.
When I get a message I need to bring the activity to front. I use the following code:
public void BringToFront()
{
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
intent.setClass(getApplicationContext(), getClass());
startActivity(intent);
}
When I get the activity on top the previous data is lost, even though the activity was not killed by the OS. Is there a way to get the activity back without losing the data or I need to recover the data anyway?
Thanks
Your code doesn't work. Your Activity is only active when it's in front. If it's not in front, it can't process so your method will never be called. An Activity should always save the data it wants to keep, either in onSaveInstanceState or when the data is changed. There's many ways to save data between activities.
If you want your activity to be in front, you'll have to close all activities that are on top of it. If you need these activities again, you should have them save their data, and start them again with the new data.
I suggest you read the documentation on how Activities work....
Just use this method in your activity as per your situation. it will work.
protected void moveToFront() {
if (Build.VERSION.SDK_INT >= 11) { // honeycomb
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final List<RunningTaskInfo> recentTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (int i = 0; i < recentTasks.size(); i++)
{
Log.d("Executed app", "Application executed : "
+recentTasks.get(i).baseActivity.toShortString()
+ "\t\t ID: "+recentTasks.get(i).id+"");
// bring to front
if (recentTasks.get(i).baseActivity.toShortString().indexOf("Your app pckg name") > -1) {
activityManager.moveTaskToFront(recentTasks.get(i).id, ActivityManager.MOVE_TASK_WITH_HOME);
}
}
}
}
Also you need to add below permission in manifest.
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.REORDER_TASKS" />