Detect which app has been launched in android

后端 未结 3 1083
孤城傲影
孤城傲影 2021-01-21 21:54

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

3条回答
  •  一向
    一向 (楼主)
    2021-01-21 22:25

    Try this code:

      ActivityManager activityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
        List 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();
            }
        }
    

提交回复
热议问题