下面列出了android.app.PendingIntent#send ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected final void newSatisfiedState(Boolean newState) {
lck_satisfied.lock();
try {
if (satisfied == newState) {
return;
}
satisfied = newState;
if (satisfied == null)
return;
PendingIntent pendingIntent = satisfied ? event_positive : event_negative;
try {
pendingIntent.send();
} catch (PendingIntent.CanceledException e) {
Logger.wtf("PendingIntent for notify in SkeletonTracker cancelled before sending???");
e.printStackTrace();
}
} finally {
lck_satisfied.unlock();
}
}
@Test
public void test_fullUpdateReciever()
{
// test PendingIntent
final SuntimesActivity activity = (SuntimesActivity)activityRule.getActivity();
PendingIntent fullUpdateIntent = activity.getFullUpdateIntent(activity);
try {
fullUpdateIntent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
fail("CanceledException!");
}
// test receiver
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
activity.fullUpdateReceiver.onReceive(activity, new Intent(SuntimesActivity.SUNTIMES_APP_UPDATE_FULL));
activity.finish();
assertTrue("app hasn't crashed", activity.isFinishing());
}
});
}
private void handleStatusChange(PlaylistItem item) {
if (item == null) {
item = mSessionManager.getCurrentItem();
}
if (item != null) {
PendingIntent receiver = item.getUpdateReceiver();
if (receiver != null) {
Intent intent = new Intent();
intent.putExtra(MediaControlIntent.EXTRA_SESSION_ID, item.getSessionId());
intent.putExtra(MediaControlIntent.EXTRA_ITEM_ID, item.getItemId());
intent.putExtra(MediaControlIntent.EXTRA_ITEM_STATUS,
item.getStatus().asBundle());
try {
receiver.send(getContext(), 0, intent);
Log.d(TAG, mRouteId + ": Sending status update from provider");
} catch (PendingIntent.CanceledException e) {
Log.d(TAG, mRouteId + ": Failed to send status update!");
}
}
}
}
/** 打开通知栏消息*/
private void openNotify(AccessibilityEvent event)
{
if (event.getParcelableData() == null || !(event.getParcelableData() instanceof Notification))
{
return;
}
Notification notification = (Notification) event.getParcelableData();
PendingIntent pendingIntent = notification.contentIntent;
try
{
pendingIntent.send();
}
catch (PendingIntent.CanceledException e)
{
e.printStackTrace();
}
}
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);
}
}
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);
}
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();
}
}
/**
* 返回SoloPi
*/
public void moveSelfToFront() {
int contextFrom = 0;
// 一级一级加载Context
Context context = loadActivityOnTop();
if (context == null) {
context = loadRunningService();
contextFrom = 1;
}
if (context == null) {
context = getApplicationContext();
contextFrom = 2;
}
if (contextFrom != 0) {
//获取ActivityManager
ActivityManager mAm = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
//获得当前运行的task
List<ActivityManager.RunningTaskInfo> taskList = mAm.getRunningTasks(100);
for (ActivityManager.RunningTaskInfo rti : taskList) {
//找到当前应用的task,并启动task的栈顶activity,达到程序切换到前台
if (rti.topActivity.getPackageName().equals(getPackageName())) {
mAm.moveTaskToFront(rti.id, 0);
return;
}
}
// pending intent跳回去
Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName());
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
try {
pendingIntent.send();
} catch (PendingIntent.CanceledException e) {
LogUtil.e(TAG, "Catch android.app.PendingIntent.CanceledException: " + e.getMessage(), e);
}
}
}
private void returnPendingIntentWithError(PendingIntent pendingIntent) {
try {
pendingIntent.send(mContext, SmsManager.MMS_ERROR_UNSPECIFIED, null);
} catch (PendingIntent.CanceledException e) {
Slog.e(TAG, "Failed to return pending intent result", e);
}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static void openNotification(AccessibilityEvent event) {
if( !(event.getParcelableData() instanceof Notification)) {
return;
}
Notification notification = (Notification) event.getParcelableData();
PendingIntent pendingIntent = notification.contentIntent;
try {
pendingIntent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
}
public void onClickStartRfidReader(View v) {
Log.i("onClickStartRfidReader", "Yay I was clicked.");
mFullName.setText("Start RFID Reader button pushed!");
if (intents.containsKey("gov.nasa.arc.irg.test_rfid_reader")) {
PendingIntent start_apk_intent = intents.get("gov.nasa.arc.irg.test_rfid_reader");
try {
start_apk_intent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
} else {
Log.e("MainActivity", "Couldn't start rfid apk since we don't have the pending intent.");
}
}
/**
* Creates an OnClickListener that triggers the specified PendingIntent.
*
* @hide
*/
public static OnClickListener createIntentOnClickListener(@NonNull final PendingIntent intent) {
Preconditions.checkNotNull(intent);
return v -> {
try {
intent.send();
} catch (PendingIntent.CanceledException e) {
Log.e(LOG_TAG, "Error sending PendingIntent", e);
}
};
}
/**
* Perform the operation associated with this PendingIntent.
*/
public static boolean sendPendingIntent(@Nullable PendingIntent pi, Context context, Intent intent) {
if (pi != null)
try {
// The Context of the caller may be null if
// <var>intent</var> is also null.
Check.getInstance().isTrue(context != null || intent == null);
//noinspection ConstantConditions
pi.send(context, 0, intent);
return true;
} catch (PendingIntent.CanceledException e) { /* unused */ }
return false;
}
private void dismissAfterSharing(PendingIntent originalIntent) {
try {
originalIntent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
if (isOrderedBroadcast()) {
try {
abortBroadcast();
} catch (RuntimeException ignored) {
}
}
}
}
/**
* 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 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.");
}
}
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.");
}
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
Notification notification = sbn.getNotification();
if (null == notification) return;
Bundle extras = notification.extras;
if (null == extras) return;
List<String> textList = new ArrayList<>();
String title = extras.getString("android.title");
if (!isEmpty(title)) textList.add(title);
String detailText = extras.getString("android.text");
if (!isEmpty(detailText)) textList.add(detailText);
if (textList.size() == 0) return;
for (String text : textList) {
if (!isEmpty(text) && text.contains("[微信红包]")) {
final PendingIntent pendingIntent = notification.contentIntent;
try {
pendingIntent.send();
} catch (PendingIntent.CanceledException e) {
}
break;
}
}
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Intent nextIntent = new Intent(context, SavedNewsService.class);
nextIntent.setAction(SavedNewsService.ACTION_GET_NEXT);
nextIntent.putExtra(SavedNewsService.PARAM_CURRENT, -1);
PendingIntent nextPendingIntent = PendingIntent.getService(context, 0, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
try {
nextPendingIntent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
}
/**
* Finishes activity with {@link Activity#RESULT_OK}.
*
* @param pattern
* the pattern, if this is in mode creating pattern. In any
* cases, it can be set to {@code null}.
*/
private void finishWithResultOk(char[] pattern) {
if (ACTION_CREATE_PATTERN.equals(getIntent().getAction()))
mIntentResult.putExtra(EXTRA_PATTERN, pattern);
else {
/*
* If the user was "logging in", minimum try count can not be zero.
*/
mIntentResult.putExtra(EXTRA_RETRY_COUNT, mRetryCount + 1);
}
setResult(RESULT_OK, mIntentResult);
/*
* ResultReceiver
*/
ResultReceiver receiver = getIntent().getParcelableExtra(
EXTRA_RESULT_RECEIVER);
if (receiver != null) {
Bundle bundle = new Bundle();
if (ACTION_CREATE_PATTERN.equals(getIntent().getAction()))
bundle.putCharArray(EXTRA_PATTERN, pattern);
else {
/*
* If the user was "logging in", minimum try count can not be
* zero.
*/
bundle.putInt(EXTRA_RETRY_COUNT, mRetryCount + 1);
}
receiver.send(RESULT_OK, bundle);
}
/*
* PendingIntent
*/
PendingIntent pi = getIntent().getParcelableExtra(
EXTRA_PENDING_INTENT_OK);
if (pi != null) {
try {
pi.send(this, RESULT_OK, mIntentResult);
} catch (Throwable t) {
Log.e(CLASSNAME, "Error sending PendingIntent: " + pi, t);
}
}
finish();
}