下面列出了android.os.PersistableBundle#putPersistableBundle ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public PersistableBundle getPersistableBundle() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
PersistableBundle persistableBundle = new PersistableBundle();
persistableBundle.putLong("id", id);
persistableBundle.putDouble("latitude", latitude);
persistableBundle.putDouble("longitude", longitude);
persistableBundle.putInt("orderId", orderId);
persistableBundle.putString("locale", localeAbbrev);
persistableBundle.putString("nickname", nickname);
persistableBundle.putDouble("accuracy", new Double(accuracy));
persistableBundle.putString("locationSource", locationSource);
persistableBundle.putLong("lastLocationUpdate", lastLocationUpdate);
persistableBundle.putPersistableBundle("address", PersistableBundleBuilder.fromAddress(address));
return persistableBundle;
} else {
return null;
}
}
public static PersistableBundle serializedDataToPersistableBundle(List<?> args, Context context) {
final ComponentName name = new ComponentName(context, AndroidJobScheduler.class);
final Integer every = (Integer) args.get(0);
final String funcCallback = (String) args.get(1);
final Integer id = (Integer) args.get(2);
final Map<String, Map<String, Object>> constraints = (Map<String, Map<String, Object>>) args.get(3);
PersistableBundle job = new PersistableBundle();
job.putString(B_KEY_RESCHEDULE, funcCallback);
job.putInt(B_KEY_ID, id);
job.putInt(B_KEY_INTERVAL, every);
job.putString(B_KEY_DART_CB, funcCallback);
job.putString(B_KEY_COMPONENT_PKG, name.getPackageName());
job.putString(B_KEY_COMPONENT_NAME, name.getClassName());
if (constraints != null) {
for (Map.Entry<String, Map<String, Object>> entry : constraints.entrySet()) {
PersistableBundle innerBundle = new PersistableBundle();
for (Map.Entry<String, Object> innerEntry : entry.getValue().entrySet()) {
innerBundle.putInt(innerEntry.getKey(), (Integer) innerEntry.getValue());
}
job.putPersistableBundle(entry.getKey(), innerBundle);
}
}
return job;
}
@VisibleForTesting
static JobInfo createJobInfoFromTaskInfo(Context context, TaskInfo taskInfo) {
PersistableBundle jobExtras = new PersistableBundle();
jobExtras.putString(BACKGROUND_TASK_CLASS_KEY, taskInfo.getBackgroundTaskClass().getName());
PersistableBundle persistableBundle = getTaskExtrasAsPersistableBundle(taskInfo);
jobExtras.putPersistableBundle(BACKGROUND_TASK_EXTRAS_KEY, persistableBundle);
JobInfo.Builder builder =
new JobInfo
.Builder(taskInfo.getTaskId(),
new ComponentName(context, BackgroundTaskJobService.class))
.setExtras(jobExtras)
.setPersisted(taskInfo.isPersisted())
.setRequiresCharging(taskInfo.requiresCharging())
.setRequiredNetworkType(getJobInfoNetworkTypeFromTaskNetworkType(
taskInfo.getRequiredNetworkType()));
if (taskInfo.isPeriodic()) {
builder = getPeriodicJobInfo(builder, taskInfo);
} else {
builder = getOneOffJobInfo(builder, taskInfo);
}
return builder.build();
}
@TargetApi(VERSION_CODES.LOLLIPOP_MR1)
public static PersistableBundle bundleToPersistableBundle(Bundle bundle) {
Set<String> keySet = bundle.keySet();
PersistableBundle persistableBundle = new PersistableBundle();
for (String key : keySet) {
Object value = bundle.get(key);
if (value instanceof Boolean) {
persistableBundle.putBoolean(key, (boolean) value);
} else if (value instanceof Integer) {
persistableBundle.putInt(key, (int) value);
} else if (value instanceof String) {
persistableBundle.putString(key, (String) value);
} else if (value instanceof String[]) {
persistableBundle.putStringArray(key, (String[]) value);
} else if (value instanceof Bundle) {
PersistableBundle innerBundle = bundleToPersistableBundle((Bundle) value);
persistableBundle.putPersistableBundle(key, innerBundle);
}
}
return persistableBundle;
}
private void saveSystemUpdateInfoLocked(PersistableBundle infoBundle, int uid) {
// Wrap the incoming bundle with extra info (e.g. version, uid, boot count). We use nested
// PersistableBundle to avoid manually parsing XML attributes when loading the info back.
PersistableBundle outBundle = new PersistableBundle();
outBundle.putPersistableBundle(KEY_INFO_BUNDLE, infoBundle);
outBundle.putInt(KEY_VERSION, INFO_FILE_VERSION);
outBundle.putInt(KEY_UID, uid);
outBundle.putInt(KEY_BOOT_COUNT, getBootCount());
// Only update the info on success.
if (writeInfoFileLocked(outBundle)) {
mLastUid = uid;
mLastStatus = infoBundle.getInt(KEY_STATUS);
}
}
private PersistableBundle deepCopyBundle(PersistableBundle bundle, int maxDepth) {
if (maxDepth <= 0) {
return null;
}
PersistableBundle copy = (PersistableBundle) bundle.clone();
Set<String> keySet = bundle.keySet();
for (String key: keySet) {
Object o = copy.get(key);
if (o instanceof PersistableBundle) {
PersistableBundle bCopy = deepCopyBundle((PersistableBundle) o, maxDepth-1);
copy.putPersistableBundle(key, bCopy);
}
}
return copy;
}
PersistableBundle toPersistableBundle() {
PersistableBundle bundle = new PersistableBundle();
bundle.putString("type", type);
bundle.putString("title", title);
bundle.putString("icon", icon);
bundle.putPersistableBundle("userInfo", userInfo.toPersistableBundle());
return bundle;
}
/**
* All fields are stored in a corresponding key in the persistable bundle.
*
* {@link #extras} is a Bundle and can contain parcelable objects. But only the type Account
* is allowed {@link ContentResolver#validateSyncExtrasBundle(Bundle)} that can't be stored in
* a PersistableBundle. For every value of type Account with key 'key', we store a
* PersistableBundle containing account information at key 'ACCOUNT:key'. The Account object
* can be reconstructed using this.
*
* We put a flag with key 'SyncManagerJob', to identify while reconstructing a sync operation
* from a bundle whether the bundle actually contains information about a sync.
* @return A persistable bundle containing all information to re-construct the sync operation.
*/
PersistableBundle toJobInfoExtras() {
// This will be passed as extras bundle to a JobScheduler job.
PersistableBundle jobInfoExtras = new PersistableBundle();
PersistableBundle syncExtrasBundle = new PersistableBundle();
for (String key: extras.keySet()) {
Object value = extras.get(key);
if (value instanceof Account) {
Account account = (Account) value;
PersistableBundle accountBundle = new PersistableBundle();
accountBundle.putString("accountName", account.name);
accountBundle.putString("accountType", account.type);
// This is stored in jobInfoExtras so that we don't override a user specified
// sync extra with the same key.
jobInfoExtras.putPersistableBundle("ACCOUNT:" + key, accountBundle);
} else if (value instanceof Long) {
syncExtrasBundle.putLong(key, (Long) value);
} else if (value instanceof Integer) {
syncExtrasBundle.putInt(key, (Integer) value);
} else if (value instanceof Boolean) {
syncExtrasBundle.putBoolean(key, (Boolean) value);
} else if (value instanceof Float) {
syncExtrasBundle.putDouble(key, (double) (float) value);
} else if (value instanceof Double) {
syncExtrasBundle.putDouble(key, (Double) value);
} else if (value instanceof String) {
syncExtrasBundle.putString(key, (String) value);
} else if (value == null) {
syncExtrasBundle.putString(key, null);
} else {
Slog.e(TAG, "Unknown extra type.");
}
}
jobInfoExtras.putPersistableBundle("syncExtras", syncExtrasBundle);
jobInfoExtras.putBoolean("SyncManagerJob", true);
jobInfoExtras.putString("provider", target.provider);
jobInfoExtras.putString("accountName", target.account.name);
jobInfoExtras.putString("accountType", target.account.type);
jobInfoExtras.putInt("userId", target.userId);
jobInfoExtras.putInt("owningUid", owningUid);
jobInfoExtras.putString("owningPackage", owningPackage);
jobInfoExtras.putInt("reason", reason);
jobInfoExtras.putInt("source", syncSource);
jobInfoExtras.putBoolean("allowParallelSyncs", allowParallelSyncs);
jobInfoExtras.putInt("jobId", jobId);
jobInfoExtras.putBoolean("isPeriodic", isPeriodic);
jobInfoExtras.putInt("sourcePeriodicId", sourcePeriodicId);
jobInfoExtras.putLong("periodMillis", periodMillis);
jobInfoExtras.putLong("flexMillis", flexMillis);
jobInfoExtras.putLong("expectedRuntime", expectedRuntime);
jobInfoExtras.putInt("retries", retries);
jobInfoExtras.putInt("syncExemptionFlag", syncExemptionFlag);
return jobInfoExtras;
}