下面列出了android.support.v4.app.BundleCompat#putBinder ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Open the site settings for given url in the web browser. The url must belong to the origin
* associated with the calling application via the Digital Asset Links. Prior to calling, one
* must establish a connection to {@link CustomTabsService} and create a
* {@link CustomTabsSession}.
*
* It is also required to do {@link CustomTabsClient#warmup} and
* {@link CustomTabsSession#validateRelationship} before calling this method.
*
* @param context {@link Context} to use while launching site-settings activity.
* @param session The {@link CustomTabsSession} used to verify the origin.
* @param uri The {@link Uri} for which site-settings are to be shown.
*/
public static void launchBrowserSiteSettings(Context context, CustomTabsSession session,
Uri uri) {
Intent intent = new Intent(TrustedWebUtils.ACTION_MANAGE_TRUSTED_WEB_ACTIVITY_DATA);
intent.setPackage(session.getComponentName().getPackageName());
intent.setData(uri);
Bundle bundle = new Bundle();
BundleCompat.putBinder(bundle, CustomTabsIntent.EXTRA_SESSION, session.getBinder());
intent.putExtras(bundle);
PendingIntent id = session.getId();
if (id != null) {
intent.putExtra(CustomTabsIntent.EXTRA_SESSION_ID, id);
}
context.startActivity(intent);
}
/**
* Inserts a {@link Binder} value into an Intent as an extra.
*
* Uses {@link BundleCompat#putBinder()}, but doesn't throw exceptions.
*
* @param intent Intent to put the binder into.
* @param name Key.
* @param binder Binder object.
*/
@VisibleForTesting
public static void safePutBinderExtra(Intent intent, String name, IBinder binder) {
if (intent == null) return;
Bundle bundle = new Bundle();
try {
BundleCompat.putBinder(bundle, name, binder);
} catch (Throwable t) {
// Catches parceling exceptions.
Log.e(TAG, "putBinder failed on bundle " + bundle);
}
intent.putExtras(bundle);
}
/**
* Inserts a {@link Binder} value into an Intent as an extra.
*
* Uses {@link BundleCompat#putBinder()}, but doesn't throw exceptions.
*
* @param intent Intent to put the binder into.
* @param name Key.
* @param binder Binder object.
*/
@VisibleForTesting
public static void safePutBinderExtra(Intent intent, String name, IBinder binder) {
if (intent == null) return;
Bundle bundle = new Bundle();
try {
BundleCompat.putBinder(bundle, name, binder);
} catch (Throwable t) {
// Catches parceling exceptions.
Log.e(TAG, "putBinder failed on bundle " + bundle);
}
intent.putExtras(bundle);
}
private void setSessionParameters(@Nullable IBinder session,
@Nullable PendingIntent sessionId) {
Bundle bundle = new Bundle();
BundleCompat.putBinder(bundle, EXTRA_SESSION, session);
if (sessionId != null) {
bundle.putParcelable(EXTRA_SESSION_ID, sessionId);
}
mIntent.putExtras(bundle);
}
/**
* Inserts a {@link Binder} value into an Intent as an extra.
*
* Uses {@link BundleCompat#putBinder()}, but doesn't throw exceptions.
*
* @param intent Intent to put the binder into.
* @param name Key.
* @param binder Binder object.
*/
@VisibleForTesting
public static void safePutBinderExtra(Intent intent, String name, IBinder binder) {
if (intent == null) return;
Bundle bundle = new Bundle();
try {
BundleCompat.putBinder(bundle, name, binder);
} catch (Throwable t) {
// Catches parceling exceptions.
Log.e(TAG, "putBinder failed on bundle " + bundle);
}
intent.putExtras(bundle);
}
@Test
public void testDeserializeIBinder() {
IBinder expectedValue = new Binder();
BundleCompat.putBinder(bundle(), randomKey(), expectedValue);
assertEquals(expectedValue, mCoder.deserialize(bundle(), randomKey()));
}
/**
* Creates a {@link CustomTabsIntent.Builder} object associated with a given
* {@link CustomTabsSession}.
*
* Guarantees that the {@link Intent} will be sent to the same component as the one the
* session is associated with.
*
* @param session The session to associate this Builder with.
*/
public Builder(@Nullable CustomTabsSession session) {
if (session != null) mIntent.setPackage(session.getComponentName().getPackageName());
Bundle bundle = new Bundle();
BundleCompat.putBinder(
bundle, EXTRA_SESSION, session == null ? null : session.getBinder());
mIntent.putExtras(bundle);
}
/**
* Creates a {@link CustomTabsIntent.Builder} object associated with a given
* {@link CustomTabsSession}.
*
* Guarantees that the {@link Intent} will be sent to the same component as the one the
* session is associated with.
*
* @param session The session to associate this Builder with.
*/
public Builder(@Nullable CustomTabsSession session) {
if (session != null) mIntent.setPackage(session.getComponentName().getPackageName());
Bundle bundle = new Bundle();
BundleCompat.putBinder(
bundle, EXTRA_SESSION, session == null ? null : session.getBinder());
mIntent.putExtras(bundle);
}
/**
* Creates a {@link CustomTabsIntent.Builder} object associated with a given
* {@link CustomTabsSession}.
*
* Guarantees that the {@link Intent} will be sent to the same component as the one the
* session is associated with.
*
* @param session The session to associate this Builder with.
*/
public Builder(@Nullable CustomTabsSession session) {
if (session != null) mIntent.setPackage(session.getComponentName().getPackageName());
Bundle bundle = new Bundle();
BundleCompat.putBinder(
bundle, EXTRA_SESSION, session == null ? null : session.getBinder());
mIntent.putExtras(bundle);
}
/**
* Creates a {@link CustomTabsIntent.Builder} object associated with a given
* {@link CustomTabsSession}.
*
* Guarantees that the {@link Intent} will be sent to the same component as the one the
* session is associated with.
*
* @param session The session to associate this Builder with.
*/
public Builder(@Nullable CustomTabsSession session) {
if (session != null) mIntent.setPackage(session.getComponentName().getPackageName());
Bundle bundle = new Bundle();
BundleCompat.putBinder(
bundle, EXTRA_SESSION, session == null ? null : session.getBinder());
mIntent.putExtras(bundle);
}
/**
* Write a field's value into the saved state {@link Bundle}.
*
* @param state {@link Bundle} used to save the state
* @param key key retrieved from {@code fieldDeclaringClass#fieldName}
* @param fieldValue value of field
*/
@Override
public void serialize(@NonNull Bundle state, @NonNull String key, @NonNull IBinder fieldValue) {
BundleCompat.putBinder(state, key, fieldValue);
}