i trying to make an android app, that verify if certain app is running, if the certain app is not running my app have to open it, but i don´t know how to do for to verify if the certain app is running? can somebody help me please...
Asked
Active
Viewed 57 times
0
-
Please be more specific. – apollosoftware.org Mar 14 '17 at 17:25
-
1http://stackoverflow.com/questions/4212992/how-can-i-check-if-an-app-running-on-android See the accepted answer. – ale.m Mar 14 '17 at 17:27
-
or here: http://stackoverflow.com/a/22503513/1771254 – pinedax Mar 14 '17 at 17:29
1 Answers
0
you can use TaskManager see this Sample Code http://forum.xda-developers.com/attachment.php?attachmentid=2955965&stc=1&d=1412353638
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final List<RunningTaskInfo> recentTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
/**recentTasks.get(0) Activity that is on top >> your app and number 1 will next **/
String pack = recentTasks.get(0).baseActivity.toShortString();
also you can get info of running app with UsageStats like this
try{
AppOpsManager appOps = (AppOpsManager) getSystemService(APP_OPS_SERVICE);
int mode = appOps.checkOp("android:get_usage_stats", android.os.Process.myUid(), getPackageName());
if(mode == AppOpsManager.MODE_ALLOWED) {//should turn on UsageStats
UsageStatsManager mUsageStatsManager = (UsageStatsManager)getSystemService(USAGE_STATS_SERVICE);
List<UsageStats> queryUsageStats = mUsageStatsManager
.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,
System.currentTimeMillis() - 1000*60, //begin
System.currentTimeMillis()); //end
Collections.sort(queryUsageStats, new LastTimeLaunchedComparator());
try{
CustomUsageStats customUsageStats = new CustomUsageStats();
customUsageStats.usageStats = queryUsageStats.get(1);
String previousAppPack = customUsageStats.usageStats.getPackageName();
//the package of app before your app
}
catch (Exception e){}
}

Elias Fazel
- 2,093
- 16
- 20