How to tell if a user is on Device Home Screen - android

I have a app that displays a chat head bubble bubble similar to the Facebook messenger app. I want to be able to have it only appear when the user is in the home screen and not follow you when you go to other apps. Is there anyway to know when the user is in the device home screen? Couldn't find much information on this can anyone point me to the right direction if this is even possible? Thank you

You can do it be getting the running tasks and filter them by using Intent.CATEGORY_HOME and Intent.ACTION_MAIN like that:
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);
ComponentName currentTask = taskInfo.get(0).topActivity;
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_HOME);
mPackageManager = getPackageManager();
List<ResolveInfo> appList = mPackageManager.queryIntentActivities(mainIntent, 0);
for(int i = 0; i < appList.size(); i++) {
ResolveInfo apl = appList.get(i);
if(currentTask.getPackageName().equals(apl.activityInfo.packageName)) {
Log.e(TAG, "ONHOMESCREEN");
}
}
and also add this to your manifest:
<uses-permission android:name="android.permission.GET_TASKS"/>

Related

Is there any way to detect whether the installed application is in use or not?

I know we can detect the installed application but is there any way to detect that which application is in use or which is in idle/closed state?
For example, if I open the Whatsapp, it can detect the WhatsApp and Toast me a message "using WhatsApp".
You cannot detect an App launch in Android, but you can get the list of currently open apps and check if the app you're looking for is open or not using the following code:
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();
for (int i = 0; i < runningAppProcessInfo.size(); i++) {
if(runningAppProcessInfo.get(i).processName.equals("com.the.app.you.are.looking.for")) {
// Do your stuff here.
}
}
You can also check if the app is running in the foreground using this method
public static boolean isForeground(Context ctx, String myPackage){
ActivityManager manager = (ActivityManager) ctx.getSystemService(ACTIVITY_SERVICE);
List< ActivityManager.RunningTaskInfo > runningTaskInfo = manager.getRunningTasks(1);
ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
if(componentInfo.getPackageName().equals(myPackage)) {
return true;
}
return false;
}
this response is from this post :
How to get the list of running applications?

Android check only desktop is running

i'm programing simple SMS manager and my application can view dialog for receive new sms like with GoSMS, now i want to disable it when other application is running and i want to view this dialog when no program is open and launcher is active. i can use this below code to detect it but if user installed any launcher how i can know.
ActivityManager am = (ActivityManager) G.context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
String currentRunningActivityName = taskInfo.get(0).topActivity.getClassName();
Log.i(TAG, currentRunningActivityName);
if (currentRunningActivityName.equals("com.android.launcher2.Launcher")) {
///do your task
}
You can use this snippet, this method can find all launcher and if that return true then desktop's launcher is running by default:
public static Boolean isHomeScreen() {
ActivityManager am = (ActivityManager) G.context.getSystemService( Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
String currentActivity = taskInfo.get(0).topActivity.getClassName();
PackageManager pm = G.context.getPackageManager();
Intent homeIntent = new Intent(Intent.ACTION_MAIN, null);
homeIntent.addCategory(Intent.CATEGORY_DEFAULT);
homeIntent.addCategory(Intent.CATEGORY_HOME);
homeIntent.addCategory(Intent.CATEGORY_MONKEY);
List<ResolveInfo> appList = pm.queryIntentActivities(homeIntent, 0);
for (ResolveInfo temp: appList) {
if (temp.activityInfo.name.equals(currentActivity))
return true;
}
return false;
}

Opening recent apps on button click

I have a button in a service where the user clicks on it the recent apps dialog will appear, but I don't know a proper way to achieve that, can anyone help?
ActivityManager has method getrunningtasks() .
ActivityManager activity_manager = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);
then call the getRunningtasks() to get a list of the current Running tasks :
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 + "");
}
and please add the permission to get the running tasks in your manifest file like the following :
<uses-permission android:name="android.permission.GET_TASKS"/>
Hope that helps .
Recent apps can be opened using the TOGGLE_RECENTS Intent.
Intent intent = new Intent ("com.android.systemui.recent.action.TOGGLE_RECENTS");
intent.setComponent (new ComponentName ("com.android.systemui", "com.android.systemui.recent.RecentsActivity");
startActivity (intent);
You can make your own dialog to show recent apps
ActivityManager am = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);
then you can get all running apps with
getRunningTasks

How to launch an app from another app

in my app i have to launch another app, only if it is not yet launched.
To launch an app what i do is:
PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage("package.of.app.to.launch");
startActivity(intent);
My problem is that if app is already launched, it will bring the app to the front. How can i launch app only if is not present in active and launched apps? thanks!
You can get a list of all running or active apps and then check if the App you want to start is in it.
ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> runningProcInfo = activityManager.getRunningAppProcesses();
for(int i = 0; i < runningProcInfo.size(); i++){
if(runningProcInfo.get(i).processName.equals("package.of.app.to.launch")) {
Log.i(TAG, "XXXX is running");
} else {
// RUN THE CODE TO START THE ACTIVITY
}
}
You can use ActivityManager to get Currently running Applications and to get recent task executed on Device as:
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> recentTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (int i = 0; i < recentTasks.size(); i++)
{
// Here you can put your logic for executing new task
}
If you want to know whether an app is running or not, you should have a look at getRunningTasks
Also this post might help you.
you can get that which activity is running
List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);
String currentActivity=taskInfo.get(0).topActivity.getPackageName();
then check
if(currentActivity!=(your activity))
{
**your intent**
}
else{
"app already running"
}
hope it helps.
thank you

How do I know if the home screen is in focus?

I have an service running on Android and I need to know if is any application on focus or if the "desktop" (home screen) is in focus. I don't know if this is the proper word to refer to the home screen of the phone. How can I know if this is in focus or some other application?
Inside the service I have this code to get the running tasks:
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
// get the info from the currently running task
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
How can I know from componentInfo if this is desktop or no? On emulator the componentInfo.getPackageName() returns com.android.launcher but in a Galaxy S1 (I tested only in this phone) returns something else.
There is any other way to do this?
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);
ComponentName currentTask = taskInfo.get(0).topActivity;
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_HOME);
mPackageManager = getPackageManager();
List<ResolveInfo> appList = mPackageManager.queryIntentActivities(mainIntent, 0);
for(int i = 0; i < appList.size(); i++) {
ResolveInfo apl = appList.get(i);
if(currentTask.getPackageName().equals(apl.activityInfo.packageName)) {
Log.e(TAG, "ONHOMESCREEN");
}
}
And in AndroidManifest.xml you need permission
<uses-permission android:name="android.permission.GET_TASKS"/>
Try this function,
public boolean isUserIsOnHomeScreen()
{
ActivityManager manager =
(ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> processes = manager.getRunningAppProcesses();
for (RunningAppProcessInfo process : processes)
{
if(process.pkgList[0].equalsIgnoreCase("com.android.launcher"))
{
return true;
}
else
{
return false;
}
}
return false;
}
If you want to get control when "the home screen is in focus", implement a home screen.

Categories

Resources