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.
Related
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?
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"/>
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;
}
I have an application in which I have a particular event receiver. My goal is to display a dialog box when the application is running, otherwise (ie if the application is not running) show a notification.
The events such as notifications or displaying dialog boxes I have implemented. I can not determine whether the user has the application on or off.
Any ideas ?
ActivityManager am = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> runningTaskInfoList = am.getRunningTasks(1);
ComponentName componentName = runningTaskInfoList.get(0).topActivity;
String runningActivityName = componentName.getClassName();
String runningActivityPackageName = componentName.getPackageName();
It requires "android.permission.GET_TASKS" permission.
Exact here -
ActivityManager activityManager =(ActivityManager)gpsService.this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> serviceList= activityManager.getRunningServices(Integer.MAX_VALUE);
if((serviceList.size() > 0)) {
boolean found = false;
for(int i = 0; i < serviceList.size(); i++) {
RunningServiceInfo serviceInfo = serviceList.get(i);
ComponentName serviceName = serviceInfo.service;
if(serviceName.getClassName().equals("Packagename.ActivityOrServiceName")) {
//Your service or activity is running
found = true;
break;
}
}
if(found) {
//Close your app or service
}
}
Source - how-can-i-check-if-my-app-is-running.
application still alive or not
public boolean isAppRunning()
{
boolean appFound = false;
final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
final List<RunningTaskInfo> recentTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (RunningTaskInfo recentTask : recentTasks)
{
if (recentTask.baseActivity.getPackageName().equals("package.Name"))
{
appFound = true;
break;
}
}
return appFound;
}
need permission also
<uses-permission android:name="android.permission.GET_TASKS" />
Please check one my question is related to your question. in that please check comments.
Check application is minimized/background
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