简体   繁体   English

检测哪个应用已在Android中启动

[英]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. 如何检测用户在我的应用程序中启动了哪个应用程序,即我的应用程序应该在用户启动Whatsapp时得到通知,即使我的应用程序没有在前台或后台运行。

hike messenger has achieved same functionality with accessibility service. hike messenger与辅助功能服务实现了相同的功能。

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. 根据运行应用程序的Android版本,您必须使用不同的方法。 On Pre-Lollipop devices, it is pretty straight-forward: 在Pre-Lollipop设备上,它非常简单:

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 如果您使用的是Lollipop或更新的设备,则必须使用UsageStatsManager类,这需要为您的应用程序授予特定权限

//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: 最后,这将为用户启动Android权限授予活动:

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. 不,使用公共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 您可以通过ActivityManager获取当前运行的进程#getRunningAppProcesses但是它绝对不可能得到通知。但是,它不是最准确或最有效的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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