Search code examples
androidandroid-intentlaunch

Check if app is executed by another


I want to know if my app was executed by another app

I tried several methods but I could not find a solution

I tried this but returns the same value please help me! Code:

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

Solution

  • This answer from dhaval will work for you

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

    replace com.android.browser with your app process name.