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
Related
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 trying to write a UI Automation test and I'm trying to speed it up. At one point of the test, I need to close all recent apps on a device. I can do this via UiAutomator (currently doing it that way) but this isnt the part of the test I'm focusing on so I was wondering if this is doable via an intent call?
Based on the file structure, I would prefer not to add new classes, change activity code (b/c only needed for testing), or change permissions too badly.
I want to doing something along the line of:
Intent closeIntent = new Intent(context, null);
closeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(closeIntent);
Android isnt okay with the null class parameter.
I've looked at the following but none of them are what I really want:
Finish all activities at a time
Intent data not clearing when user presses back button then recent apps button
https://gist.github.com/anonymous/9884978
Thanks in advance!
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (int i = 0; i < tasks.size(); i++) {
Log.d("Running task", "Running task: " + tasks.get(i).baseActivity.toShortString() + "\t\t ID: " + tasks.get(i).id);
String packageName = processus.processName.split(":")[0];
if (!getPackageName().equals(packageName) && !tasks.contains(packageName)) {
am.killBackgroundProcesses(packageName);
}
}
Hope this will help you out.....
How can i show alert in any one of running activities of third party app by initializing service (finds and show alert) in mainactivity of third party app and it needs to show my alert in one activity.
Please look at this i think we can do using this.
ArrayList<String> runningactivities = new ArrayList<String>();
ActivityManager activityManager = (ActivityManager)getBaseContext().getSystemService (Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> services = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (int i1 = 0; i1 < services.size(); i1++) {
runningactivities.add(0,services.get(i1).topActivity.toString());
}
if(runningactivities.contains("ComponentInfo{com.app/com.app.main.MyActivity}")==true){
Toast.makeText(getBaseContext(),"Activity is in foreground, active",1000).show();
alert.show()
}
please help me how can i achieve this.
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.
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