List of usage examples for android.app Activity getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
From source file:net.lp.actionbarpoirot.helpers.DualNavUtils.java
/** * Convenience method that is equivalent to calling * <code>{@link #navigateUpTo(Activity, Intent) navigateUpTo}(sourceActivity, * {@link #getParentActivityIntent(Activity) getParentActivityIntent} (sourceActivity))</code> * . sourceActivity will be finished by this call. * //www .j a v a 2 s. c o m * <p> * <em>Note:</em> This method should only be used when sourceActivity and * the corresponding parent are within the same task. If up navigation * should cross tasks in some cases, see * {@link #shouldUpRecreateTask(Activity, Intent)}. * </p> * * @param sourceActivity * The current activity from which the user is attempting to * navigate up */ public static void navigateUpFromSameTask(Activity sourceActivity) { Intent upIntent = getParentActivityIntent(sourceActivity); if (upIntent == null) { throw new IllegalArgumentException("Activity " + sourceActivity.getClass().getSimpleName() + " does not have a parent activity name specified." + " (Did you forget to add the android.support.PARENT_ACTIVITY <meta-data> " + " element in your manifest?)"); } navigateUpTo(sourceActivity, upIntent); }
From source file:com.aitorvs.android.allowme.AllowMe.java
public static void unregisterActivity(@NonNull Activity activity) { //noinspection ConstantConditions if (getInstance().mActivity == null || activity == null) { Log.w(TAG, "Trying to unregister null activity"); } else if (activity.getClass().getName().equals(getInstance().mActivity.getClass().getName())) { getInstance().mActivity = null;/* w ww . java2 s.c o m*/ } else { Log.w(TAG, "unregisterActivity: Old activity is trying to unregister"); } }
From source file:Main.java
public static boolean startActivity(Activity a, String packageName, Bundle args) { PackageManager pm = a.getPackageManager(); boolean result = true; try {// w w w.j av a 2s .c o m Intent intent = pm.getLaunchIntentForPackage(packageName); if (null != intent) { intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); if (args != null) intent.putExtras(args); a.startActivity(intent); } else { result = startActivityUsingScheme(a, packageName, args); } } catch (Exception e) { Log.e(a.getClass().getName(), e.getMessage(), e); result = startActivityUsingScheme(a, packageName, args); } return result; }
From source file:com.example.mynsocial.BluetoothChat.java
public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) { Log.i(TAG, "---------------------------------------"); final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass()); intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);/* www . j ava 2 s .c om*/ String[][] techList = new String[][] {}; adapter.enableForegroundDispatch(activity, pendingIntent, null, techList); }
From source file:org.solovyev.android.sherlock.AndroidSherlockUtils.java
@Nonnull public static ActionBar getSupportActionBar(@Nonnull Activity activity) { if (activity instanceof SherlockActivity) { return ((SherlockActivity) activity).getSupportActionBar(); }/* w w w. j a v a2 s .c o m*/ if (activity instanceof SherlockFragmentActivity) { return ((SherlockFragmentActivity) activity).getSupportActionBar(); } if (activity instanceof SherlockListActivity) { return ((SherlockListActivity) activity).getSupportActionBar(); } if (activity instanceof SherlockPreferenceActivity) { return ((SherlockPreferenceActivity) activity).getSupportActionBar(); } throw new IllegalArgumentException(activity.getClass() + " is not supported!"); }
From source file:org.solovyev.android.sherlock.AndroidSherlockUtils.java
@Nonnull public static MenuInflater getSupportMenuInflater(@Nonnull Activity activity) { if (activity instanceof SherlockActivity) { return ((SherlockActivity) activity).getSupportMenuInflater(); }//from w ww.j a v a 2s . c o m if (activity instanceof SherlockFragmentActivity) { return ((SherlockFragmentActivity) activity).getSupportMenuInflater(); } if (activity instanceof SherlockListActivity) { return ((SherlockListActivity) activity).getSupportMenuInflater(); } if (activity instanceof SherlockPreferenceActivity) { return ((SherlockPreferenceActivity) activity).getSupportMenuInflater(); } throw new IllegalArgumentException(activity.getClass() + " is not supported!"); }
From source file:com.hemou.android.account.AccountUtils.java
/** * Get account used for authentication/*from w ww.j ava2s. com*/ * * @param manager * @param act * @return account * @throws IOException * @throws AccountsException */ public static Account getAccount(final AccountManager manager, final Activity act) throws IOException, AccountsException { final String SUB_TAG = "acnt_get"; final boolean loggable = Log.isLoggable(SUB_TAG, DEBUG); if (loggable) Log.d(SUB_TAG, "Getting account"); if (act == null) throw new IllegalArgumentException("Activity cannot be null"); if (act.isFinishing()) { Log.v(SUB_TAG, act.getClass().getName() + "--->?finish()OperationCanceledException..."); throw new OperationCanceledException(); } Account[] accounts; try { if (!hasAuthenticator(manager)) { Log.e(SUB_TAG, "Current user is not under the authenticated environment...."); throw new AuthenticatorConflictException(); } while ((accounts = getAccounts(manager)).length == 0) { Bundle result = manager.addAccount(ACCOUNT_TYPE, AUTHTOKEN_TYPE, null, null, act, null, null) .getResult(); } } catch (OperationCanceledException e) { Log.d(SUB_TAG, "Excepting retrieving account", e); act.finish(); throw e; } catch (AccountsException e) { Log.d(SUB_TAG, "Excepting retrieving account", e); throw e; } catch (AuthenticatorConflictException e) { act.runOnUiThread(new Runnable() { public void run() { showConflictMessage(act); } }); throw e; } catch (IOException e) { Log.d(SUB_TAG, "Excepting retrieving account", e); throw e; } // if (loggable) Log.d(SUB_TAG, "Returning account " + accounts[0].name); return accounts[0]; }
From source file:de.uni_koblenz_landau.apow.LoginActivity.java
/** * Start scanning for NFC tags./*from www .j a v a2 s.c om*/ * @param activity The Activity requesting to the foreground dispatch. * @param adapter The NfcAdapter used for the foreground dispatch. */ private static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) { if (adapter != null) { // Add intents for NFC. final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass()); intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0); IntentFilter[] filters = new IntentFilter[1]; String[][] techList = new String[][] {}; filters[0] = new IntentFilter(); filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED); filters[0].addCategory(Intent.CATEGORY_DEFAULT); try { filters[0].addDataType(MIME_TEXT_PLAIN); } catch (MalformedMimeTypeException e) { e.printStackTrace(); } adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList); } }
From source file:it.imwatch.nfclottery.MainActivity.java
/** * Sets up the dispatching of NFC events a the foreground Activity. * * @param activity The {@link android.app.Activity} to setup the foreground dispatch for * @param adapter The {@link android.nfc.NfcAdapter} to setup the foreground dispatch for *//*from www . ja va 2s . c o m*/ public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) { if (adapter != null) { final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass()); intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0); IntentFilter[] filters = new IntentFilter[1]; String[][] techList = new String[][] {}; // Notice that this is the same filter we define in our manifest. // This ensures we're not stealing events for other types of NDEF. filters[0] = new IntentFilter(); IntentFilter filter = filters[0]; filter.addAction(NfcAdapter.ACTION_NDEF_DISCOVERED); filter.addCategory(Intent.CATEGORY_DEFAULT); try { filter.addDataType(MIME_VCARD); filter.addDataType(MIME_XVCARD); } catch (IntentFilter.MalformedMimeTypeException e) { throw new RuntimeException("Mime type not supported."); } adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList); } }
From source file:cat.ereza.customactivityoncrash.CustomActivityOnCrash.java
/** * Installs CustomActivityOnCrash on the application using the default error activity. * * @param context Context to use for obtaining the ApplicationContext. Must not be null. *///from w w w.j a v a 2 s.c om public static void install(Context context) { try { if (context == null) { Log.e(TAG, "Install failed: context is null!"); } else { if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) { Log.w(TAG, "CustomActivityOnCrash will be installed, but may not be reliable in API lower than 14"); } //INSTALL! final Thread.UncaughtExceptionHandler oldHandler = Thread.getDefaultUncaughtExceptionHandler(); if (oldHandler != null && oldHandler.getClass().getName().startsWith(CAOC_HANDLER_PACKAGE_NAME)) { Log.e(TAG, "You have already installed CustomActivityOnCrash, doing nothing!"); } else { if (oldHandler != null && !oldHandler.getClass().getName().startsWith(DEFAULT_HANDLER_PACKAGE_NAME)) { Log.e(TAG, "IMPORTANT WARNING! You already have an UncaughtExceptionHandler, are you sure this is correct? If you use ACRA, Crashlytics or similar libraries, you must initialize them AFTER CustomActivityOnCrash! Installing anyway, but your original handler will not be called."); } application = (Application) context.getApplicationContext(); //We define a default exception handler that does what we want so it can be called from Crashlytics/ACRA Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread thread, final Throwable throwable) { Log.e(TAG, "App has crashed, executing CustomActivityOnCrash's UncaughtExceptionHandler", throwable); if (hasCrashedInTheLastSeconds(application)) { Log.e(TAG, "App already crashed in the last 2 seconds, not starting custom error activity because we could enter a restart loop. Are you sure that your app does not crash directly on init?", throwable); if (oldHandler != null) { oldHandler.uncaughtException(thread, throwable); return; } } else { setLastCrashTimestamp(application, new Date().getTime()); if (errorActivityClass == null) { errorActivityClass = guessErrorActivityClass(application); } if (isStackTraceLikelyConflictive(throwable, errorActivityClass)) { Log.e(TAG, "Your application class or your error activity have crashed, the custom activity will not be launched!"); if (oldHandler != null) { oldHandler.uncaughtException(thread, throwable); return; } } else if (launchErrorActivityWhenInBackground || !isInBackground) { final Intent intent = new Intent(application, errorActivityClass); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); throwable.printStackTrace(pw); String stackTraceString = sw.toString(); //Reduce data to 128KB so we don't get a TransactionTooLargeException when sending the intent. //The limit is 1MB on Android but some devices seem to have it lower. //See: http://developer.android.com/reference/android/os/TransactionTooLargeException.html //And: http://stackoverflow.com/questions/11451393/what-to-do-on-transactiontoolargeexception#comment46697371_12809171 if (stackTraceString.length() > MAX_STACK_TRACE_SIZE) { String disclaimer = " [stack trace too large]"; stackTraceString = stackTraceString.substring(0, MAX_STACK_TRACE_SIZE - disclaimer.length()) + disclaimer; } if (enableAppRestart && restartActivityClass == null) { //We can set the restartActivityClass because the app will terminate right now, //and when relaunched, will be null again by default. restartActivityClass = guessRestartActivityClass(application); } else if (!enableAppRestart) { //In case someone sets the activity and then decides to not restart restartActivityClass = null; } String userLogString = ""; while (!userLogs.isEmpty()) { userLogString += userLogs.poll(); } intent.putExtra(EXTRA_STACK_TRACE, stackTraceString); intent.putExtra(EXTRA_USER_ACTION_TRACE, userLogString); intent.putExtra(EXTRA_RESTART_ACTIVITY_CLASS, restartActivityClass); intent.putExtra(EXTRA_SHOW_ERROR_DETAILS, showErrorDetails); intent.putExtra(EXTRA_EVENT_LISTENER, eventListener); intent.putExtra(EXTRA_IMAGE_DRAWABLE_ID, defaultErrorActivityDrawableId); intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); if (eventListener != null) { eventListener.onLaunchErrorActivity(); } application.startActivity(intent); } } final Activity lastActivity = lastActivityCreated.get(); if (lastActivity != null) { //We finish the activity, this solves a bug which causes infinite recursion. //This is unsolvable in API<14, so beware! //See: https://github.com/ACRA/acra/issues/42 lastActivity.finish(); lastActivityCreated.clear(); } killCurrentProcess(); } }); if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { application .registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() { int currentlyStartedActivities = 0; DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US); @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) { if (activity.getClass() != errorActivityClass) { // Copied from ACRA: // Ignore activityClass because we want the last // application Activity that was started so that we can // explicitly kill it off. lastActivityCreated = new WeakReference<>(activity); } userLogs.add(dateFormat.format(new Date()) + " " + activity.getLocalClassName() + " created\n"); } @Override public void onActivityStarted(Activity activity) { currentlyStartedActivities++; isInBackground = (currentlyStartedActivities == 0); //Do nothing } @Override public void onActivityResumed(Activity activity) { userLogs.add(dateFormat.format(new Date()) + " " + activity.getLocalClassName() + " resumed\n"); } @Override public void onActivityPaused(Activity activity) { userLogs.add(dateFormat.format(new Date()) + " " + activity.getLocalClassName() + " paused\n"); } @Override public void onActivityStopped(Activity activity) { //Do nothing currentlyStartedActivities--; isInBackground = (currentlyStartedActivities == 0); } @Override public void onActivitySaveInstanceState(Activity activity, Bundle outState) { //Do nothing } @Override public void onActivityDestroyed(Activity activity) { userLogs.add(dateFormat.format(new Date()) + " " + activity.getLocalClassName() + " destroyed\n"); } }); } Log.i(TAG, "CustomActivityOnCrash has been installed."); } } } catch (Throwable t) { Log.e(TAG, "An unknown error occurred while installing CustomActivityOnCrash, it may not have been properly initialized. Please report this as a bug if needed.", t); } }