Start App in various ways - Android App

Android examples for App:App Running

Description

Start App in various ways

Demo Code

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;

public class Main {
  private static final String LOG_TAG = "";

  public static boolean startApp(Context context, Class<?> launcherActivityClass) {
    try {//from   w w  w  .java2  s. co m
      Intent intent;

      if (isAppRunning(context)) {
        intent = new Intent(context, launcherActivityClass);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
      } else {
        String packageName = context.getPackageName();
        intent = context.getPackageManager().getLaunchIntentForPackage(
            packageName);
        if (intent != null) {
          intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          intent.setAction(Intent.ACTION_MAIN);
          intent.addCategory(Intent.CATEGORY_LAUNCHER);
          context.startActivity(intent);
        } else {
          return false;
        }
      }
    } catch (ActivityNotFoundException e) {

      return false;
    }

    return true;
  }

  public static boolean startApp(Context context, String packageName,
      String className) {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setClassName(packageName, className);

    try {
      context.startActivity(intent);
    } catch (ActivityNotFoundException e) {

      return false;
    }

    return true;
  }

  public static boolean startApp(Context context, String packageName,
      String className, Map<String, String> data) {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setClassName(packageName, className);

    if (data != null) {
      Iterator<Entry<String, String>> iter = data.entrySet().iterator();
      while (iter.hasNext()) {
        Entry<String, String> entry = iter.next();
        intent.putExtra(entry.getKey(), entry.getValue());
      }
    }

    try {
      context.startActivity(intent);
    } catch (ActivityNotFoundException e) {

      return false;
    }

    return true;
  }

  public static boolean startApp(Context context, String packageName) {
    try {
      Intent intent = context.getPackageManager().getLaunchIntentForPackage(
          packageName);
      if (intent != null) {
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        context.startActivity(intent);
      } else {

        return false;
      }
    } catch (ActivityNotFoundException e) {

      return false;
    }

    return true;
  }

  public static boolean isAppRunning(Context context) {
    boolean isRunning = false;

    ActivityManager am = (ActivityManager) context
        .getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> list = am.getRunningTasks(100);
    if (list.size() <= 0) {
      return false;
    }

    String packageName = context.getPackageName();
    for (RunningTaskInfo info : list) {
      if (info.topActivity.getPackageName().equals(packageName)) {
        isRunning = true;
        break;
      }
    }

    return isRunning;
  }
}

Related Tutorials