0

I am using this code to open one app from another:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    PackageManager pm = getPackageManager();
    Intent intent = pm.getLaunchIntentForPackage("com.example.p2a");
    intent.putExtra("application_id", 2);
    getApplicationContext().startActivity(intent);
    finish();
}

How I can check if this another app is running? I want to If running- kill and start again, if no-start app.

user1302569
  • 7,131
  • 13
  • 46
  • 66

1 Answers1

1

You can check the app's running status through this

ActivityManager aManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
    List<RunningAppProcessInfo> processInfo = aManager.getRunningAppProcesses();
    for(int i = 0; i < processInfo.size(); i++){
        if(processInfo.get(i).processName.equals("myPackage.myAppName")) {
            //Kill app
        }
    }
     //Start app
MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • @user1302569, I haven't tried that yet. Check http://stackoverflow.com/a/12037227/1777090 , http://stackoverflow.com/a/4504690/1777090. That might help. – MysticMagicϡ Dec 14 '12 at 12:07