下面列出了怎么用android.app.PendingIntent.CanceledException的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void testOnStartShouldSignOutIfConfigurationHasChanged() throws CanceledException, JSONException {
// Create new configuration to change the hash
Context context = RuntimeEnvironment.application.getApplicationContext();
new OAuthClientConfiguration(
context,
context.getSharedPreferences(OAuthClientConfiguration.PREFS_NAME, MODE_PRIVATE),
ConfigurationStreams.getOtherConfiguration()
);
doNothing().when(mCancelIntent).send();
OktaManagementActivity activity = Robolectric.buildActivity(
OktaManagementActivity.class
).newIntent(createStartIntent()).create().start().get();
assertThat(activity.isFinishing()).isTrue();
}
@Test
public void testOnStartShouldCompleteIfStateIsAuthorized() throws CanceledException, JSONException {
Context context = RuntimeEnvironment.application.getApplicationContext();
AuthStateManager stateManager = AuthStateManager.getInstance(context);
stateManager.replace(mAuthState);
when(mAuthState.isAuthorized()).thenReturn(true);
doNothing().when(mCompleteIntent).send();
OktaManagementActivity activity = Robolectric.buildActivity(
OktaManagementActivity.class,
createStartIntent()
).create().start().get();
assertThat(activity.isFinishing()).isTrue();
}
private void handleAuthorizationComplete() {
Uri responseUri = getIntent().getData();
Intent responseData = extractResponseData(responseUri);
if (responseData == null) {
Logger.error("Failed to extract OAuth2 response from redirect");
return;
}
responseData.setData(responseUri);
if (mCompleteIntent != null) {
Logger.debug("Authorization complete - invoking completion intent");
try {
mCompleteIntent.send(this, 0, responseData);
} catch (CanceledException ex) {
Logger.error("Failed to send completion intent", ex);
}
} else {
setResult(RESULT_OK, responseData);
}
}
private void handleAuthorizationCanceled() {
Logger.debug("Authorization flow canceled by user");
Intent cancelData = AuthorizationException.fromTemplate(
AuthorizationException.GeneralErrors.USER_CANCELED_AUTH_FLOW,
null)
.toIntent();
if (mCancelIntent != null) {
try {
mCancelIntent.send(this, 0, cancelData);
} catch (CanceledException ex) {
Logger.error("Failed to send cancel intent", ex);
}
} else {
setResult(RESULT_CANCELED, cancelData);
Logger.debug("No cancel intent set - will return to previous activity");
}
}
/**
* Triggers the client-defined action when the user clicks a custom menu item.
* @param menuIndex The index that the menu item is shown in the result of
* {@link #getMenuTitles()}
*/
public void clickMenuItemWithUrl(ChromeActivity activity, int menuIndex, String url) {
Intent addedIntent = new Intent();
addedIntent.setData(Uri.parse(url));
try {
// Media viewers pass in PendingIntents that contain CHOOSER Intents. Setting the data
// in these cases prevents the Intent from firing correctly.
String title = mMenuEntries.get(menuIndex).first;
PendingIntent pendingIntent = mMenuEntries.get(menuIndex).second;
pendingIntent.send(
activity, 0, isMediaViewer() ? null : addedIntent, mOnFinished, null);
if (shouldEnableEmbeddedMediaExperience()
&& TextUtils.equals(
title, activity.getString(R.string.download_manager_open_with))) {
RecordUserAction.record("CustomTabsMenuCustomMenuItem.DownloadsUI.OpenWith");
}
} catch (CanceledException e) {
Log.e(TAG, "Custom tab in Chrome failed to send pending intent.");
}
}
/**
* testGetIntentSender
*/
private void testGetIntentSender() {
Intent intent = new Intent();
intent.setClass(this, AnimationAlphaActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 100, intent, 0);
try {
pi.send();
} catch (CanceledException e) {
if (DEBUG) {
e.printStackTrace();
}
}
}
/**
* Triggers the client-defined action when the user clicks a custom menu item.
* @param menuIndex The index that the menu item is shown in the result of
* {@link #getMenuTitles()}
*/
public void clickMenuItemWithUrl(ChromeActivity activity, int menuIndex, String url) {
Intent addedIntent = new Intent();
addedIntent.setData(Uri.parse(url));
try {
PendingIntent pendingIntent = mMenuEntries.get(menuIndex).second;
pendingIntent.send(activity, 0, addedIntent, mOnFinished, null);
} catch (CanceledException e) {
Log.e(TAG, "Custom tab in Chrome failed to send pending intent.");
}
}
/**
* Sends the pending intent for the custom button on toolbar with the given url as data.
* @param context The context to use for sending the {@link PendingIntent}.
* @param url The url to attach as additional data to the {@link PendingIntent}.
*/
public void sendButtonPendingIntentWithUrl(Context context, String url) {
Intent addedIntent = new Intent();
addedIntent.setData(Uri.parse(url));
try {
getCustomButtonOnToolbar().getPendingIntent().send(context, 0, addedIntent, mOnFinished,
null);
} catch (CanceledException e) {
Log.e(TAG, "CanceledException while sending pending intent in custom tab");
}
}
private static void sendPendingIntentWithUrl(PendingIntent pendingIntent, Intent extraIntent,
ChromeActivity activity) {
Intent addedIntent = extraIntent == null ? new Intent() : new Intent(extraIntent);
Tab tab = activity.getActivityTab();
if (tab != null) addedIntent.setData(Uri.parse(tab.getUrl()));
try {
pendingIntent.send(activity, 0, addedIntent, null, null);
} catch (CanceledException e) {
Log.e(TAG, "CanceledException when sending pending intent.");
}
}
/**
* Triggers the client-defined action when the user clicks a custom menu item.
* @param menuIndex The index that the menu item is shown in the result of
* {@link #getMenuTitles()}
*/
public void clickMenuItemWithUrl(ChromeActivity activity, int menuIndex, String url) {
Intent addedIntent = new Intent();
addedIntent.setData(Uri.parse(url));
try {
// Media viewers pass in PendingIntents that contain CHOOSER Intents. Setting the data
// in these cases prevents the Intent from firing correctly.
PendingIntent pendingIntent = mMenuEntries.get(menuIndex).second;
pendingIntent.send(
activity, 0, isMediaViewer() ? null : addedIntent, mOnFinished, null);
} catch (CanceledException e) {
Log.e(TAG, "Custom tab in Chrome failed to send pending intent.");
}
}
/**
* Sends the pending intent for the custom button on toolbar with the given url as data.
* @param context The context to use for sending the {@link PendingIntent}.
* @param url The url to attach as additional data to the {@link PendingIntent}.
*/
public void sendButtonPendingIntentWithUrl(Context context, String url) {
Intent addedIntent = new Intent();
addedIntent.setData(Uri.parse(url));
try {
getCustomButtonOnToolbar().getPendingIntent().send(context, 0, addedIntent, mOnFinished,
null);
} catch (CanceledException e) {
Log.e(TAG, "CanceledException while sending pending intent in custom tab");
}
}
private static void sendPendingIntentWithUrl(PendingIntent pendingIntent, Intent extraIntent,
ChromeActivity activity) {
Intent addedIntent = extraIntent == null ? new Intent() : new Intent(extraIntent);
Tab tab = activity.getActivityTab();
if (tab != null) addedIntent.setData(Uri.parse(tab.getUrl()));
try {
pendingIntent.send(activity, 0, addedIntent, null, null);
} catch (CanceledException e) {
Log.e(TAG, "CanceledException when sending pending intent.");
}
}
/**
* <p>
* GPS开关
* <p>
* 当前若关则打开
* <p>
* 当前若开则关闭
*/
public static void toggleGPS(Context context) {
Intent gpsIntent = new Intent();
gpsIntent.setClassName("com.android.settings",
"com.android.settings.widget.SettingsAppWidgetProvider");
gpsIntent.addCategory("android.intent.category.ALTERNATIVE");
gpsIntent.setData(Uri.parse("custom:3"));
try {
PendingIntent.getBroadcast(context, 0, gpsIntent, 0).send();
} catch (CanceledException e) {
e.printStackTrace();
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
BrowserActionItem menuItem = mMenuItems.get(position);
if (menuItem.getAction() != null) {
try {
menuItem.getAction().send();
} catch (CanceledException e) {
Log.e(TAG, "Failed to send custom item action", e);
}
} else if (menuItem.getRunnableAction() != null) {
menuItem.getRunnableAction().run();
}
mBrowserActionsDialog.dismiss();
}
/**
* Sends the pending intent for the custom button on toolbar with the given url as data.
* @param context The context to use for sending the {@link PendingIntent}.
* @param url The url to attach as additional data to the {@link PendingIntent}.
*/
public void sendButtonPendingIntentWithUrl(Context context, String url) {
Intent addedIntent = new Intent();
addedIntent.setData(Uri.parse(url));
try {
getCustomButtonOnToolbar().getPendingIntent().send(context, 0, addedIntent, mOnFinished,
null);
} catch (CanceledException e) {
Log.e(TAG, "CanceledException while sending pending intent in custom tab");
}
}
private static void sendPendingIntentWithUrl(PendingIntent pendingIntent, Intent extraIntent,
ChromeActivity activity) {
Intent addedIntent = extraIntent == null ? new Intent() : new Intent(extraIntent);
Tab tab = activity.getActivityTab();
if (tab != null) addedIntent.setData(Uri.parse(tab.getUrl()));
try {
pendingIntent.send(activity, 0, addedIntent, null, null);
} catch (CanceledException e) {
Log.e(TAG, "CanceledException when sending pending intent.");
}
}
/**
* Called when a custom item of Browser action menu is selected.
* @param action The PendingIntent action to be launched.
*/
public void onCustomItemSelected(PendingIntent action) {
try {
action.send();
} catch (CanceledException e) {
Log.e(TAG, "Browser Action in Chrome failed to send pending intent.");
}
}
/**
* Given an authorization token request intent and authorization information ({@code authToken}
* and {@code authType}) issues a response.
*/
static void issueAuthTokenResponse(Context context, PendingIntent pendingIntent, String authToken,
String authType) {
Intent responseIntent = new Intent()
.putExtra(AuthTokenConstants.EXTRA_AUTH_TOKEN, authToken)
.putExtra(AuthTokenConstants.EXTRA_AUTH_TOKEN_TYPE, authType);
try {
pendingIntent.send(context, 0, responseIntent);
} catch (CanceledException exception) {
logger.warning("Canceled auth request: %s", exception);
}
}
private void saveData()
{
if ( !PicturePasswordUtils.saveUnlockData( this, mBitmap, mGridSize, mRandomize, mChosenNumber, mUnlockPosition ) )
{
// uh oh
finish();
}
else
{
PendingIntent requestedIntent = getIntent().getParcelableExtra( "PendingIntent" );
boolean ok = false;
if ( requestedIntent != null )
{
try
{
requestedIntent.send();
ok = true;
}
catch ( CanceledException e )
{
ok = false;
}
}
else
{
Log.e( "PicturePassword", "PendingIntent was null or canceled! This is probably bad!" );
Intent chooseIntent = new Intent();
chooseIntent.setClassName( "com.android.settings", "com.android.settings.ChooseLockGeneric" );
chooseIntent.putExtra( "lockscreen.biometric_weak_fallback", true );
startActivity( chooseIntent );
}
finish();
}
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
mContext = context;
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.small_widget_layout);
Intent intent = new Intent(context, WidgetButtonService.class);
intent.putExtra("msg", mReading + "");
intent.putExtra("widget", "yes");
intent.setAction(ACTION_UPDATEUI);
Intent clickIntent = new Intent(context, BarometerNetworkActivity.class);
PendingIntent clickPI = PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.widgetSmallText, clickPI);
remoteViews.setOnClickPendingIntent(R.id.widget_tendency_image_down, clickPI);
remoteViews.setOnClickPendingIntent(R.id.widget_tendency_image_steady, clickPI);
remoteViews.setOnClickPendingIntent(R.id.widget_tendency_image_up, clickPI);
PendingIntent actionPendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.widgetSmallSubmitButton, actionPendingIntent);
try {
actionPendingIntent.send();
} catch(CanceledException ce) {
//log(ce.getMessage());
}
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
public void onClick(View v) {
Context context = IntentActivityFlags.this;
PendingIntent pi = PendingIntent.getActivities(context, 0,
buildIntentsToViewsLists(), PendingIntent.FLAG_UPDATE_CURRENT);
try {
pi.send();
} catch (CanceledException e) {
Log.w("IntentActivityFlags", "Failed sending PendingIntent", e);
}
}
/**
* Given an authorization token request intent and authorization information ({@code authToken}
* and {@code authType}) issues a response.
*/
static void issueAuthTokenResponse(Context context, PendingIntent pendingIntent, String authToken,
String authType) {
Intent responseIntent = new Intent()
.putExtra(AuthTokenConstants.EXTRA_AUTH_TOKEN, authToken)
.putExtra(AuthTokenConstants.EXTRA_AUTH_TOKEN_TYPE, authType);
try {
pendingIntent.send(context, 0, responseIntent);
} catch (CanceledException exception) {
logger.warning("Canceled auth request: %s", exception);
}
}
/**
* Given an authorization token request intent and authorization information ({@code authToken}
* and {@code authType}) issues a response.
*/
static void issueAuthTokenResponse(Context context, PendingIntent pendingIntent, String authToken,
String authType) {
Intent responseIntent = new Intent()
.putExtra(AuthTokenConstants.EXTRA_AUTH_TOKEN, authToken)
.putExtra(AuthTokenConstants.EXTRA_AUTH_TOKEN_TYPE, authType);
try {
pendingIntent.send(context, 0, responseIntent);
} catch (CanceledException exception) {
logger.warning("Canceled auth request: %s", exception);
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
CardPresenter cardPresenter = mCardPresenters.get(position);
PendingIntent pendingIntent = cardPresenter.getPendingIntent();
if (pendingIntent != null) {
try {
pendingIntent.send();
} catch (CanceledException e) {
Log.w(TAG, e.getMessage());
}
} else {
Log.w(TAG, "No PendingIntent attached to card!");
}
}
private void finish(int startId, PendingIntent pendingIntent, int result) {
if (pendingIntent != null) {
try {
pendingIntent.send(result);
} catch (CanceledException e) {
// Ignore this. If they don't want their result, they don't get it.
}
}
this.stopSelfResult(startId);
}