start Activity Safely - Android android.app

Android examples for android.app:Activity Start

Description

start Activity Safely

Demo Code

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.ActivityInfo;

public class Main {

  public static boolean startActivitySafely(Activity from, Intent intent) {
    try {/*  w  w w  .  jav  a 2  s  .  c  o m*/
      from.startActivity(intent);
      return true;
    } catch (ActivityNotFoundException e) {
    } catch (SecurityException e) {
    }

    return false;
  }

  public static boolean startActivitySafely(Activity from, Intent intent, ActivityInfo appInfo) {
    @SuppressWarnings("unused")
    CharSequence app_name = appInfo.applicationInfo.loadLabel(from.getPackageManager());

    try {
      intent.setPackage(appInfo.packageName);
      intent.setClassName(appInfo.packageName, appInfo.name);

      from.startActivity(intent);
      return true;
    } catch (ActivityNotFoundException e) {
    } catch (SecurityException e) {
    }

    return false;
  }

}

Related Tutorials