下面列出了怎么用android.view.WindowManager.BadTokenException的API类实例代码及写法,或者点击链接到github查看源代码。
public void displayText(CharSequence text) {
if (TextUtils.isEmpty(text)) {
hide();
return;
}
try {
show();
} catch (BadTokenException e) {
LogUtils.e(LOG_TAG, e, "Caught WindowManager.BadTokenException while displaying text.");
}
final long displayTime = Math.max(2000, text.length() * 100);
mHandler.removeMessages(MSG_CLEAR_TEXT);
mText.setText(text.toString().trim());
mHandler.sendEmptyMessageDelayed(MSG_CLEAR_TEXT, displayTime);
}
/** Override focus highlighting with a custom overlay */
public void addViewAndShow(View view) {
relativeLayout.addView(view);
try {
// TODO: Investigate removing this try/catch block. Exceptions occur in PointScan
// when starting or shutting down. Since the exceptions occur after the service is connected
// and before the service is unbound, the service isn't recognized as shutting down.
ThreadUtils.runOnMainThread(SwitchAccessService::isActive, highlightOverlay::show);
// The configuration has to happen after the overlay is drawn to ensure the dimensions are
// measured properly which doesn't happen until the next frame, hence this runs after a delay.
ThreadUtils.runOnMainThreadDelayed(
SwitchAccessService::isActive,
() -> configureOverlayAfterShow(highlightOverlay),
TIME_BETWEEN_DRAWING_OVERLAY_CONFIGURING_MS);
} catch (BadTokenException | IllegalStateException e) {
// Do nothing, as this can happen when the service is starting or shutting down.
LogUtils.d(TAG, "Couldn't show highlight overlay: %s", e);
}
}
private void showGlobalMenu() {
// TODO: Investigate if this try/catch can be removed. Putting this on the main
// thread can still cause a BadTokenException before SwitchAccessService#onUnbind is called.
try {
// Only add a delay with point scan. Any delays needed by other scanning methods will be
// handled in OptionManager.
long timeToDelay =
SwitchAccessPreferenceUtils.isPointScanEnabled(getContext())
? POINT_SCAN_DELAY_BETWEEN_ADJUSTING_MENU_BUTTON_POSITION_AND_SHOWING_MS
: 0;
ThreadUtils.runOnMainThreadDelayed(
SwitchAccessService::isActive,
() -> {
globalMenuButtonOverlay.show();
if (globalMenuButtonListener != null) {
globalMenuButtonListener.onGlobalMenuButtonShown();
}
},
timeToDelay);
} catch (BadTokenException | IllegalStateException e) {
// Do nothing, as this can happen when the service is starting or shutting down.
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
try {
progressDialog = new ProgressDialog(context);
progressDialog.setMessage(context.getString(R.string.msg_big_image_loading));
progressDialog.setCancelable(true);
progressDialog.setOnCancelListener(onCancelListener);
progressDialog.setOwnerActivity(context);
progressDialog.setMax(100);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.show();
if (isHit) {
cancel(true);
onPostExecute(null);
}
} catch (BadTokenException e) {
if (Logger.isDebug()) {
Log.d(LOG_TAG, e.getMessage(), e);
}
cancel(true);
}
}
/**
* {@inheritDoc}
*/
@MainThread
@Override
public void showSoftInput(int flags, ResultReceiver resultReceiver) {
if (DEBUG) Log.v(TAG, "showSoftInput()");
boolean wasVis = isInputViewShown();
if (dispatchOnShowInputRequested(flags, false)) {
try {
showWindow(true);
} catch (BadTokenException e) {
// We have ignored BadTokenException here since Jelly Bean MR-2 (API Level 18).
// We could ignore BadTokenException in InputMethodService#showWindow() instead,
// but it may break assumptions for those who override #showWindow() that we can
// detect errors in #showWindow() by checking BadTokenException.
// TODO: Investigate its feasibility. Update JavaDoc of #showWindow() of
// whether it's OK to override #showWindow() or not.
}
}
clearInsetOfPreviousIme();
// If user uses hard keyboard, IME button should always be shown.
mImm.setImeWindowStatus(mToken, mStartInputToken,
mapToImeWindowStatus(isInputViewShown()), mBackDisposition);
if (resultReceiver != null) {
resultReceiver.send(wasVis != isInputViewShown()
? InputMethodManager.RESULT_SHOWN
: (wasVis ? InputMethodManager.RESULT_UNCHANGED_SHOWN
: InputMethodManager.RESULT_UNCHANGED_HIDDEN), null);
}
}
public void showWindow(boolean showInput) {
if (DEBUG) Log.v(TAG, "Showing window: showInput=" + showInput
+ " mShowInputRequested=" + mShowInputRequested
+ " mWindowAdded=" + mWindowAdded
+ " mWindowCreated=" + mWindowCreated
+ " mWindowVisible=" + mWindowVisible
+ " mInputStarted=" + mInputStarted
+ " mShowInputFlags=" + mShowInputFlags);
if (mInShowWindow) {
Log.w(TAG, "Re-entrance in to showWindow");
return;
}
try {
mWindowWasVisible = mWindowVisible;
mInShowWindow = true;
showWindowInner(showInput);
} catch (BadTokenException e) {
// BadTokenException is a normal consequence in certain situations, e.g., swapping IMEs
// while there is a DO_SHOW_SOFT_INPUT message in the IIMethodWrapper queue.
if (DEBUG) Log.v(TAG, "BadTokenException: IME is done.");
mWindowVisible = false;
mWindowAdded = false;
// Rethrow the exception to preserve the existing behavior. Some IMEs may have directly
// called this method and relied on this exception for some clean-up tasks.
// TODO: Give developers a clear guideline of whether it's OK to call this method or
// InputMethodService#requestShowSelf(int) should always be used instead.
throw e;
} finally {
// TODO: Is it OK to set true when we get BadTokenException?
mWindowWasVisible = true;
mInShowWindow = false;
}
}
private static Runnable getWrappedRunnable(Shutdownable shutdownable, Runnable runnable) {
return () -> {
try {
runnable.run();
} catch (BadTokenException | IllegalStateException exception) {
ignoreExceptionIfShuttingDown(
shutdownable,
exception,
"Exception while trying to run runnable on main thread during shutdown. Ignoring.");
} finally {
wrappedRunnableMap.remove(runnable);
}
};
}
/** Show the screen switch overlay */
public void showScreenSwitch() {
try {
ThreadUtils.runOnMainThread(SwitchAccessService::isActive, screenSwitchOverlay::show);
} catch (BadTokenException | IllegalStateException e) {
// TODO: Investigate the root cause of this issue.
// Do nothing -- this prevents the Set-Up Wizard from crashing if there is an issue with
// initially showing the screen switch. The BadTokenException is caused when the overlay
// is shown while the fragments are transitioning while the IllegalStateException is thrown
// when the overlay is added while the overlay already exists.
LogUtils.d(TAG, "Couldn't show screen switch overlay: %s", e);
}
}
public static void alertIfPossible (final Context context, final String msg, final Exception e) {
try {
alert(context, msg, e);
}
catch (BadTokenException ignored) {/* If the UI context is no longer valid. */} // NOSONAR exception intentionally ignored.
}