Android 台灣中文網

標題: Android 跨進程傳遞對像 [打印本頁]

作者: 暗桌之光    時間: 2011-7-19 16:01
標題: Android 跨進程傳遞對像
android 跨進程傳遞對像必須實現Parcelable 「協議」,例如 public class StatusBarIcon 的對象要能夠實現傳遞功能,必須如此寫:
  1. /*
  2. * Copyright (C) 2010 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */


  15. package com.android.internal.statusbar;
  16. import android.os.Parcel;
  17. import android.os.Parcelable;

  18. /**
  19. * @hide
  20. */

  21. public class StatusBarIcon implements Parcelable {
  22. public String iconPackage;
  23. public int iconId;
  24. public int iconLevel;
  25. public boolean visible = true;
  26. public int number;
  27. private StatusBarIcon() {
  28. }
  29. public StatusBarIcon(String iconPackage, int iconId, int iconLevel) {
  30. this.iconPackage = iconPackage;
  31. this.iconId = iconId;
  32. this.iconLevel = iconLevel;
  33. }
  34. public StatusBarIcon(String iconPackage, int iconId, int iconLevel, int number) {
  35. this.iconPackage = iconPackage;
  36. this.iconId = iconId;
  37. this.iconLevel = iconLevel;
  38. this.number = number;
  39. }
  40. public String toString() {
  41. return "StatusBarIcon(pkg=" + this.iconPackage + " id=0x" + Integer.toHexString(this.iconId)
  42. + " level=" + this.iconLevel + " visible=" + visible+ " num=" + this.number + " )";
  43. }
  44. public StatusBarIcon clone() {
  45. StatusBarIcon that = new StatusBarIcon(this.iconPackage, this.iconId, this.iconLevel);
  46. that.visible = this.visible;
  47. that.number = this.number;
  48. return that;
  49. }


  50. /**
  51. * Unflatten the StatusBarIcon from a parcel.
  52. */
  53. public StatusBarIcon(Parcel in) {
  54. readFromParcel(in);
  55. }
  56. public void readFromParcel(Parcel in) {
  57. this.iconPackage = in.readString();
  58. this.iconId = in.readInt();
  59. this.iconLevel = in.readInt();
  60. this.visible = in.readInt() != 0;
  61. this.number = in.readInt();
  62. }


  63. public void writeToParcel(Parcel out, int flags) {
  64. out.writeString(this.iconPackage);
  65. out.writeInt(this.iconId);
  66. out.writeInt(this.iconLevel);
  67. out.writeInt(this.visible ? 1 : 0);
  68. out.writeInt(this.number);
  69. }


  70. public int describeContents() {
  71. return 0;
  72. }
  73. /**
  74. * Parcelable.Creator that instantiates StatusBarIcon objects
  75. */
  76. public static final Parcelable.Creator<StatusBarIcon> CREATOR
  77. = new Parcelable.Creator<StatusBarIcon>(){
  78. public StatusBarIcon createFromParcel(Parcel parcel)
  79. {
  80. return new StatusBarIcon(parcel);
  81. }
  82. public StatusBarIcon[] newArray(int size)
  83. {
  84. return new StatusBarIcon[size];
  85. }
  86. };
  87. }
複製代碼

作者: Q9Q9Q9Q9Q9    時間: 2011-7-22 10:32
感謝大大分享




歡迎光臨 Android 台灣中文網 (https://apk.tw/) Powered by Discuz! X3.1