android.os.Parcel.obtain()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(8.1k)|赞(0)|评价(0)|浏览(436)

本文整理了Java中android.os.Parcel.obtain()方法的一些代码示例,展示了Parcel.obtain()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Parcel.obtain()方法的具体详情如下:
包路径:android.os.Parcel
类名称:Parcel
方法名:obtain

Parcel.obtain介绍

[英]Retrieve a new Parcel object from the pool.
[中]从池中检索新地块对象。

代码示例

代码示例来源:origin: robolectric/robolectric

/**
 * Creates a new {@link AudioRoutesInfo} to be used for updating the Bluetooth audio route.
 *
 * @param bluetoothDeviceName the name of the Bluetooth device or null to indicate that the
 *     already-existing Bluetooth A2DP device should be removed
 */
private static AudioRoutesInfo newAudioRouteInfo(@Nullable String bluetoothDeviceName) {
 Parcel p = Parcel.obtain();
 TextUtils.writeToParcel(bluetoothDeviceName, p, /* parcelableFlags= */ 0);
 p.setDataPosition(0);
 return AudioRoutesInfo.CREATOR.createFromParcel(p);
}

代码示例来源:origin: robolectric/robolectric

private static PackageInfo newPackageInfo(PackageInfo orig) {
 Parcel parcel = Parcel.obtain();
 orig.writeToParcel(parcel, 0);
 parcel.setDataPosition(0);
 return PackageInfo.CREATOR.createFromParcel(parcel);
}

代码示例来源:origin: lingochamp/FileDownloader

public void onClickLongParcel(final View view) {
  final long start = System.currentTimeMillis();
  final LongParcel longParcel = new LongParcel();
  for (int i = 0; i < TIMES; i++) {
    Parcel p = Parcel.obtain();
    longParcel.writeToParcel(p, 0);
    LongParcel longParcelCopy = new LongParcel(p);
  }
  infoAppend("Long Parcel and Alloc [and GC]", start);
}

代码示例来源:origin: lingochamp/FileDownloader

public void onClickIntParcel(final View view) {
  final long start = System.currentTimeMillis();
  final IntParcel intParcel = new IntParcel();
  for (int i = 0; i < TIMES; i++) {
    Parcel p = Parcel.obtain();
    intParcel.writeToParcel(p, 0);
    IntParcel intParcelCopy = new IntParcel(p);
  }
  infoAppend("Int Parcel and Alloc [and GC]", start);
}

代码示例来源:origin: robolectric/robolectric

@Before
public void setup() {
 parcel = Parcel.obtain();
}

代码示例来源:origin: robolectric/robolectric

/** Returns a copy of {@code automaticZenRule}. */
private AutomaticZenRule copyAutomaticZenRule(AutomaticZenRule automaticZenRule) {
 Parcel parcel = Parcel.obtain();
 try {
  automaticZenRule.writeToParcel(parcel, /* flags= */ 0);
  parcel.setDataPosition(0);
  return new AutomaticZenRule(parcel);
 } finally {
  parcel.recycle();
 }
}

代码示例来源:origin: robolectric/robolectric

private static UserHandle newUserHandle(int uid) {
  Parcel userParcel = Parcel.obtain();
  userParcel.writeInt(uid);
  userParcel.setDataPosition(0);
  return new UserHandle(userParcel);
 }
}

代码示例来源:origin: robolectric/robolectric

@Test
 @Config(minSdk = Build.VERSION_CODES.O_MR1)
 public void writeToParcel_shouldNotCrash() throws Exception {
  try (SharedMemory sharedMemory = SharedMemory.create("foo", 4)) {
   ByteBuffer fooBuf = sharedMemory.mapReadWrite();
   fooBuf.putInt(1234);

   Parcel parcel = Parcel.obtain();
   parcel.writeParcelable(sharedMemory, 0);
  }
 }
}

代码示例来源:origin: robolectric/robolectric

private StorageVolume buildAndGetStorageVolume(File file, String description) {
  Parcel parcel = Parcel.obtain();
  parcel.writeInt(0);
  parcel.setDataPosition(0);
  UserHandle userHandle = new UserHandle(parcel);
  StorageVolumeBuilder storageVolumeBuilder =
    new StorageVolumeBuilder("volume", file, description, userHandle, "mounted");
  return storageVolumeBuilder.build();
 }
}

代码示例来源:origin: google/ExoPlayer

@Test
public void testEventMessageParcelable() {
 EventMessage eventMessage = new EventMessage("urn:test", "123", 3000, 1000403,
   new byte[] {0, 1, 2, 3, 4}, 1000);
 // Write to parcel.
 Parcel parcel = Parcel.obtain();
 eventMessage.writeToParcel(parcel, 0);
 // Create from parcel.
 parcel.setDataPosition(0);
 EventMessage fromParcelEventMessage = EventMessage.CREATOR.createFromParcel(parcel);
 // Assert equals.
 assertThat(fromParcelEventMessage).isEqualTo(eventMessage);
}

代码示例来源:origin: robolectric/robolectric

@Test
public void shouldBeParcelable() throws Exception {
 Account expected = new Account("name", "type");
 Parcel p = Parcel.obtain();
 expected.writeToParcel(p, 0);
 p.setDataPosition(0);
 Account actual = Account.CREATOR.createFromParcel(p);
 assertThat(actual).isEqualTo(expected);
}

代码示例来源:origin: robolectric/robolectric

@Test
public void shouldHaveParcelConstructor() throws Exception {
 Account expected = new Account("name", "type");
 Parcel p = Parcel.obtain();
 expected.writeToParcel(p, 0);
 p.setDataPosition(0);
 Account actual = new Account(p);
 assertThat(actual).isEqualTo(expected);
}

代码示例来源:origin: google/ExoPlayer

@Test
public void testParcelable() {
 DrmInitData drmInitDataToParcel = new DrmInitData(DATA_1, DATA_2);
 Parcel parcel = Parcel.obtain();
 drmInitDataToParcel.writeToParcel(parcel, 0);
 parcel.setDataPosition(0);
 DrmInitData drmInitDataFromParcel = DrmInitData.CREATOR.createFromParcel(parcel);
 assertThat(drmInitDataFromParcel).isEqualTo(drmInitDataToParcel);
 parcel.recycle();
}

代码示例来源:origin: google/ExoPlayer

@Test
 public void testParcelable() {
  MdtaMetadataEntry mdtaMetadataEntryToParcel =
    new MdtaMetadataEntry("test", new byte[] {1, 2}, 3, 4);

  Parcel parcel = Parcel.obtain();
  mdtaMetadataEntryToParcel.writeToParcel(parcel, 0);
  parcel.setDataPosition(0);

  MdtaMetadataEntry mdtaMetadataEntryFromParcel =
    MdtaMetadataEntry.CREATOR.createFromParcel(parcel);
  assertThat(mdtaMetadataEntryFromParcel).isEqualTo(mdtaMetadataEntryToParcel);

  parcel.recycle();
 }
}

代码示例来源:origin: robolectric/robolectric

@Test
public void shouldWriteAndReadFromParcelCorrectly() {
 Parcel p = Parcel.obtain();
 node.setContentDescription("test");
 node.writeToParcel(p, 0);
 p.setDataPosition(0);
 AccessibilityNodeInfo anotherNode = AccessibilityNodeInfo.CREATOR.createFromParcel(p);
 assertThat(node).isEqualTo(anotherNode);
 node.setContentDescription(null);
}

代码示例来源:origin: google/ExoPlayer

/** Tests {@link SelectionOverride}'s {@link android.os.Parcelable} implementation. */
@Test
public void testSelectionOverrideParcelable() {
 int[] tracks = new int[] {2, 3};
 SelectionOverride selectionOverrideToParcel =
   new SelectionOverride(/* groupIndex= */ 1, tracks);
 Parcel parcel = Parcel.obtain();
 selectionOverrideToParcel.writeToParcel(parcel, 0);
 parcel.setDataPosition(0);
 SelectionOverride selectionOverrideFromParcel =
   SelectionOverride.CREATOR.createFromParcel(parcel);
 assertThat(selectionOverrideFromParcel).isEqualTo(selectionOverrideToParcel);
 parcel.recycle();
}

代码示例来源:origin: google/ExoPlayer

@Test
 public void testParcelable() {
  Format format1 = Format.createSampleFormat("1", MimeTypes.VIDEO_H264, 0);
  Format format2 = Format.createSampleFormat("2", MimeTypes.AUDIO_AAC, 0);

  TrackGroup trackGroupToParcel = new TrackGroup(format1, format2);

  Parcel parcel = Parcel.obtain();
  trackGroupToParcel.writeToParcel(parcel, 0);
  parcel.setDataPosition(0);

  TrackGroup trackGroupFromParcel = TrackGroup.CREATOR.createFromParcel(parcel);
  assertThat(trackGroupFromParcel).isEqualTo(trackGroupToParcel);

  parcel.recycle();
 }
}

代码示例来源:origin: google/ExoPlayer

@Test
public void testParcelable() {
 Id3Frame[] subFrames = new Id3Frame[] {
  new TextInformationFrame("TIT2", null, "title"),
  new UrlLinkFrame("WXXX", "description", "url")
 };
 ChapterFrame chapterFrameToParcel = new ChapterFrame("id", 0, 1, 2, 3, subFrames);
 Parcel parcel = Parcel.obtain();
 chapterFrameToParcel.writeToParcel(parcel, 0);
 parcel.setDataPosition(0);
 ChapterFrame chapterFrameFromParcel = ChapterFrame.CREATOR.createFromParcel(parcel);
 assertThat(chapterFrameFromParcel).isEqualTo(chapterFrameToParcel);
 parcel.recycle();
}

代码示例来源:origin: ksoichiro/Android-ObservableScrollView

public void testScrollViewSavedState() throws Throwable {
  Parcel parcel = Parcel.obtain();
  ObservableScrollView.SavedState state1 = new ObservableScrollView.SavedState(AbsSavedState.EMPTY_STATE);
  state1.prevScrollY = 1;
  state1.scrollY = 2;
  state1.writeToParcel(parcel, 0);
  parcel.setDataPosition(0);
  ObservableScrollView.SavedState state2 = ObservableScrollView.SavedState.CREATOR.createFromParcel(parcel);
  assertNotNull(state2);
  assertEquals(state1.prevScrollY, state2.prevScrollY);
  assertEquals(state1.scrollY, state2.scrollY);
}

代码示例来源:origin: ksoichiro/Android-ObservableScrollView

public void testWebViewSavedState() throws Throwable {
    Parcel parcel = Parcel.obtain();
    ObservableWebView.SavedState state1 = new ObservableWebView.SavedState(AbsSavedState.EMPTY_STATE);
    state1.prevScrollY = 1;
    state1.scrollY = 2;
    state1.writeToParcel(parcel, 0);

    parcel.setDataPosition(0);

    ObservableWebView.SavedState state2 = ObservableWebView.SavedState.CREATOR.createFromParcel(parcel);
    assertNotNull(state2);
    assertEquals(state1.prevScrollY, state2.prevScrollY);
    assertEquals(state1.scrollY, state2.scrollY);
  }
}

相关文章

Parcel类方法