检查应用程序是否由另一个执行

Check if app is executed by another

我想知道我的应用程序是否被另一个应用程序执行

我尝试了几种方法,但找不到解决方案

我试过了,但是 returns 值相同,请帮助我!代码:

public class MainActivity extends Activity {

    public static boolean isinint;

    @Override
    protected void onResume() {
        super.onResume();

        Intent intent = getIntent();
        // check if any application has executed your app
        if (intent != null && intent.getType() != null) {
            isinint = true; 
            Toast.makeText(getApplicationContext(), "is:" + isinint, Toast.LENGTH_LONG).show();
        } else {
            isinint = false; 
            Toast.makeText(getApplicationContext(), "is:" + isinint, Toast.LENGTH_LONG).show();
        }
    }
}

This answer from dhaval 适合你

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("com.android.browser")) 
             {
                 Toast.makeText(getApplicationContext(), "Browser is running", Toast.LENGTH_LONG).show();
             }
         }

将 com.android.browser 替换为您的应用进程名称。