简体   繁体   中英

Detect which app has been launched in android

How to detect which app has been launched by user in my app ie my application should get notified when Whatsapp is launched by user even if my app is not running in foreground or background.

hike messenger has achieved same functionality with accessibility service.

How can I solve this problem ?

Thanks in advance!!

Try this code:

  ActivityManager activityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
    List<RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
    for(int i = 0; i < procInfos.size(); i++)
    {
        if(procInfos.get(i).processName.equals("put the package name here")) 
        {
            Toast.makeText(getApplicationContext(), "Notify Message", Toast.LENGTH_LONG).show();
        }
    }

Depending on the Android version running your application, you will have to use different methods. On Pre-Lollipop devices, it is pretty straight-forward:

String[] result = new String[2];

List<ActivityManager.RunningTaskInfo> runningTasks;
ComponentName componentInfo;

runningTasks = activityManager.getRunningTasks(1);
componentInfo = runningTasks.get(0).topActivity;
result[0] = componentInfo.getPackageName();
result[1] = componentInfo.getClassName();

If you are on a Lollipop or newer device, you have to use UsageStatsManager class, which requires your application to be granted specific permissions

//no inspection ResourceType
UsageStatsManager mUsageStatsManager = (UsageStatsManager)context.getSystemService("usagestats");
long time = System.currentTimeMillis();

// We get usage stats for the last 10 seconds
List<UsageStats> stats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000*10, time);

// Sort the stats by the last time used
if(stats != null) {
    SortedMap<Long,UsageStats> mySortedMap = new TreeMap<>();
    for (UsageStats usageStats : stats) {
        mySortedMap.put(usageStats.getLastTimeUsed(),usageStats);
    }
    if(mySortedMap != null && !mySortedMap.isEmpty()) {
        return mySortedMap.get(mySortedMap.lastKey()).getPackageName();
    }
}

return null;

This will tell you if your apps has been granted permissions:

try {
    PackageManager packageManager = context.getPackageManager();
    ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
    AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
    int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, applicationInfo.uid, applicationInfo.packageName);
    return (mode != AppOpsManager.MODE_ALLOWED);
} catch (PackageManager.NameNotFoundException e) {
    return false;
}

And finally this will launch the Android permission granting activity for the user:

Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
activity.startActivity(intent);

Hope that helps

No, this is not really possible using the public SDK.

You can get the current running process by ActivityManager#getRunningAppProcesses But it is definitely impossible to get notified .However, it isn't the most accurate, or efficient method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM