start Activity Safely - Android Activity

Android examples for Activity:Activity Start

Description

start Activity Safely

Demo Code


//package com.java2s;
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 {//from   w ww. jav a 2s  . co 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