Detect which app has been launched in android

China☆狼群 提交于 2019-12-24 00:49:16

问题


How to detect which app has been launched by user in my app i.e 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!!


回答1:


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




回答2:


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();
        }
    }



回答3:


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



来源:https://stackoverflow.com/questions/35172075/detect-which-app-has-been-launched-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!