Android How to - Start App with init Data








Question

We would like to know how to start App with init Data.

Answer

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
//from ww  w .  ja va2s  .c o m
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;

public class Main {
  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;
  }
}