下面列出了android.os.Bundle#putShort ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static DialogFragment create(boolean includeClear, boolean includeCamera, short requestCode, boolean isGroup) {
DialogFragment fragment = new AvatarSelectionBottomSheetDialogFragment();
List<SelectionOption> selectionOptions = new ArrayList<>(3);
Bundle args = new Bundle();
if (includeCamera) {
selectionOptions.add(SelectionOption.CAPTURE);
}
selectionOptions.add(SelectionOption.GALLERY);
if (includeClear) {
selectionOptions.add(SelectionOption.DELETE);
}
String[] options = Stream.of(selectionOptions)
.map(SelectionOption::getCode)
.toArray(String[]::new);
args.putStringArray(ARG_OPTIONS, options);
args.putShort(ARG_REQUEST_CODE, requestCode);
args.putBoolean(ARG_IS_GROUP, isGroup);
fragment.setArguments(args);
return fragment;
}
public static Bundle put(Bundle to, Bundle from, String key) {
if (!(to == null || from == null || TextUtils.empty(key) || !from.containsKey(key))) {
Object value = from.get(key);
if (value instanceof Boolean) {
to.putBoolean(key, ((Boolean) value).booleanValue());
} else if (value instanceof Byte) {
to.putByte(key, ((Byte) value).byteValue());
} else if (value instanceof Character) {
to.putChar(key, ((Character) value).charValue());
} else if (value instanceof Short) {
to.putShort(key, ((Short) value).shortValue());
} else if (value instanceof Integer) {
to.putInt(key, ((Integer) value).intValue());
} else if (value instanceof Long) {
to.putLong(key, ((Long) value).longValue());
} else if (value instanceof Float) {
to.putFloat(key, ((Float) value).floatValue());
} else if (value instanceof Double) {
to.putDouble(key, ((Double) value).doubleValue());
} else if (value instanceof String) {
to.putString(key, (String) value);
}
}
return to;
}
public static Bundle put(Bundle to, Intent from, String key) {
if (!(to == null || from == null || TextUtils.empty(key) || !from.hasExtra(key))) {
Object value = from.getExtras().get(key);
if (value instanceof Boolean) {
to.putBoolean(key, ((Boolean) value).booleanValue());
} else if (value instanceof Byte) {
to.putByte(key, ((Byte) value).byteValue());
} else if (value instanceof Character) {
to.putChar(key, ((Character) value).charValue());
} else if (value instanceof Short) {
to.putShort(key, ((Short) value).shortValue());
} else if (value instanceof Integer) {
to.putInt(key, ((Integer) value).intValue());
} else if (value instanceof Long) {
to.putLong(key, ((Long) value).longValue());
} else if (value instanceof Float) {
to.putFloat(key, ((Float) value).floatValue());
} else if (value instanceof Double) {
to.putDouble(key, ((Double) value).doubleValue());
} else if (value instanceof String) {
to.putString(key, (String) value);
}
}
return to;
}
private void startCallActivity(SharedPreferences sharedPreferences){
if (sharedPreferences.getBoolean(getString(R.string.SfBOnlineFlag), false) == false) {
Intent callIntent = new Intent(this, SkypeCall.class);
Bundle meetingParameters = new Bundle();
meetingParameters.putShort(getString(R.string.onlineMeetingFlag), (short) 0);
meetingParameters.putString(getString(R.string.discoveryUrl), "");
meetingParameters.putString(getString(R.string.authToken), "");
meetingParameters.putString(getString(R.string.onPremiseMeetingUrl)
, getString(R.string.meeting_url));
callIntent.putExtras(meetingParameters);
startActivity(callIntent);
} else {
mCallDoctorButton.hide();
//Start SfB Online flow
startToJoinOnlineMeeting();
}
}
/**
* Number の値を Bundle に格納します.
*
* @param bundle Number の値を格納する Bundle
* @param key キー
* @param number 格納する値
*/
protected void copyNumber(Bundle bundle, String key, Number number) {
if (number instanceof Byte) {
bundle.putByte(key, number.byteValue());
} else if (number instanceof Short) {
bundle.putShort(key, number.shortValue());
} else if (number instanceof Integer) {
bundle.putInt(key, number.intValue());
} else if (number instanceof Long) {
bundle.putLong(key, number.longValue());
} else if (number instanceof Float) {
bundle.putFloat(key, number.floatValue());
} else if (number instanceof Double) {
bundle.putDouble(key, number.doubleValue());
}
}
/**
* @param typeDef type
* @param key key
* @param value value
*/
public static void setBundleValue(Bundle bundle, Integer typeDef, String key, String value) {
if (TextUtils.isEmpty(key) || TextUtils.isEmpty(value)) {
return;
}
try {
if (null != typeDef) {
if (typeDef == Type.BOOLEAN.ordinal()) {
bundle.putBoolean(key, Boolean.parseBoolean(value));
} else if (typeDef == Type.BYTE.ordinal()) {
bundle.putByte(key, Byte.valueOf(value));
} else if (typeDef == Type.SHORT.ordinal()) {
bundle.putShort(key, Short.valueOf(value));
} else if (typeDef == Type.INT.ordinal()) {
bundle.putInt(key, Integer.valueOf(value));
} else if (typeDef == Type.LONG.ordinal()) {
bundle.putLong(key, Long.valueOf(value));
} else if (typeDef == Type.FLOAT.ordinal()) {
bundle.putFloat(key, Float.valueOf(value));
} else if (typeDef == Type.DOUBLE.ordinal()) {
bundle.putDouble(key, Double.valueOf(value));
} else if (typeDef == Type.STRING.ordinal()) {
bundle.putString(key, value);
} else if (typeDef == Type.PARCELABLE.ordinal()) {
} else if (typeDef == Type.OBJECT.ordinal()) {
bundle.putString(key, value);
} else {
bundle.putString(key, value);
}
} else {
bundle.putString(key, value);
}
} catch (Throwable ex) {
}
}
/**
* @param typeDef type
* @param key key
* @param value value
*/
public static void setBundleValue(Bundle bundle, Integer typeDef, String key, String value) {
if (TextUtils.isEmpty(key) || TextUtils.isEmpty(value)) {
return;
}
try {
if (null != typeDef) {
if (typeDef == Type.BOOLEAN.ordinal()) {
bundle.putBoolean(key, Boolean.parseBoolean(value));
} else if (typeDef == Type.BYTE.ordinal()) {
bundle.putByte(key, Byte.valueOf(value));
} else if (typeDef == Type.SHORT.ordinal()) {
bundle.putShort(key, Short.valueOf(value));
} else if (typeDef == Type.INT.ordinal()) {
bundle.putInt(key, Integer.valueOf(value));
} else if (typeDef == Type.LONG.ordinal()) {
bundle.putLong(key, Long.valueOf(value));
} else if (typeDef == Type.FLOAT.ordinal()) {
bundle.putFloat(key, Float.valueOf(value));
} else if (typeDef == Type.DOUBLE.ordinal()) {
bundle.putDouble(key, Double.valueOf(value));
} else if (typeDef == Type.STRING.ordinal()) {
bundle.putString(key, value);
} else if (typeDef == Type.PARCELABLE.ordinal()) {
} else if (typeDef == Type.OBJECT.ordinal()) {
bundle.putString(key, value);
} else {
bundle.putString(key, value);
}
} else {
bundle.putString(key, value);
}
} catch (Throwable ex) {
}
}
private void put(Bundle bundle, String name, String value) {
int type = extraTypes.getType(name);
name = extraTypes.transfer(name);
if (type == ExtraTypes.STRING) {
type = extraTypes.getType(name);
}
switch (type) {
case ExtraTypes.INT:
bundle.putInt(name, Integer.parseInt(value));
break;
case ExtraTypes.LONG:
bundle.putLong(name, Long.parseLong(value));
break;
case ExtraTypes.BOOL:
bundle.putBoolean(name, Boolean.parseBoolean(value));
break;
case ExtraTypes.SHORT:
bundle.putShort(name, Short.parseShort(value));
break;
case ExtraTypes.FLOAT:
bundle.putFloat(name, Float.parseFloat(value));
break;
case ExtraTypes.DOUBLE:
bundle.putDouble(name, Double.parseDouble(value));
break;
case ExtraTypes.BYTE:
bundle.putByte(name, Byte.parseByte(value));
break;
case ExtraTypes.CHAR:
bundle.putChar(name, value.charAt(0));
break;
default:
bundle.putString(name, value);
break;
}
}
@SuppressWarnings("unchecked")
public static Bundle parseJSONObject(JSONObject jsonObject) throws JSONException {
Bundle bundle = new Bundle();
for (Iterator<String> it = jsonObject.keys(); it.hasNext();) {
String key = it.next();
Object object = jsonObject.get(key);
if (object != null) {
if (object instanceof Integer) {
bundle.putInt(key, (Integer) object);
} else if (object instanceof int[]) {
bundle.putIntArray(key, (int[]) object);
} else if (object instanceof Long) {
bundle.putLong(key, (Long) object);
} else if (object instanceof long[]) {
bundle.putLongArray(key, (long[]) object);
} else if (object instanceof Short) {
bundle.putShort(key, (Short) object);
} else if (object instanceof short[]) {
bundle.putShortArray(key, (short[]) object);
} else if (object instanceof Byte) {
bundle.putByte(key, (Byte) object);
} else if (object instanceof byte[]) {
bundle.putByteArray(key, (byte[]) object);
} else if (object instanceof Boolean) {
bundle.putBoolean(key, (Boolean) object);
} else if (object instanceof String) {
bundle.putString(key, (String) object);
} else if (object instanceof String[]) {
bundle.putStringArray(key, (String[]) object);
} else if (object instanceof JSONObject) {
bundle.putParcelable(key, parseJSONObject((JSONObject) object));
} else if (object instanceof JSONArray) {
bundle.putParcelableArrayList(key, parseJSONArray((JSONArray) object));
}
}
}
return bundle;
}
/**
* ベンダー拡張を Bundle に格納します.
*
* @param bundle ベンダー拡張を格納するBundle
*/
@SuppressWarnings("unchecked")
protected void copyVendorExtensions(Bundle bundle) {
if (mVendorExtensions != null && !mVendorExtensions.isEmpty()) {
for (String key : mVendorExtensions.keySet()) {
Object object = mVendorExtensions.get(key);
if (object instanceof Integer) {
bundle.putInt(key, (Integer) object);
} else if (object instanceof int[]) {
bundle.putIntArray(key, (int[]) object);
} else if (object instanceof Long) {
bundle.putLong(key, (Long) object);
} else if (object instanceof long[]) {
bundle.putLongArray(key, (long[]) object);
} else if (object instanceof Short) {
bundle.putShort(key, (Short) object);
} else if (object instanceof short[]) {
bundle.putShortArray(key, (short[]) object);
} else if (object instanceof Byte) {
bundle.putByte(key, (Byte) object);
} else if (object instanceof byte[]) {
bundle.putByteArray(key, (byte[]) object);
} else if (object instanceof Boolean) {
bundle.putBoolean(key, (Boolean) object);
} else if (object instanceof String) {
bundle.putString(key, (String) object);
} else if (object instanceof String[]) {
bundle.putStringArray(key, (String[]) object);
} else if (object instanceof Bundle) {
bundle.putParcelable(key, (Bundle) object);
} else if (object instanceof ArrayList) {
bundle.putParcelableArrayList(key, (ArrayList) object);
}
}
}
}
public void putShort(Bundle state, String key, short x) {
state.putShort(key + mBaseKey, x);
}
public void putBoxedShort(Bundle state, String key, Short x) {
if (x != null) {
state.putShort(key + mBaseKey, x);
}
}
public static Bundle fromJsonToBundle(JSONObject jsonObject) {
Bundle budle = new Bundle();
if (jsonObject == null) {
return budle;
} else {
Iterator iterator = jsonObject.keySet().iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
Object value = jsonObject.get(key);
if (value != null) {
if (value instanceof String) {
budle.putString(key, (String) value);
} else if (value instanceof Byte) {
budle.putByte(key, ((Byte) value).byteValue());
} else if (value instanceof Short) {
budle.putShort(key, ((Short) value).shortValue());
} else if (value instanceof Integer) {
budle.putInt(key, ((Integer) value).intValue());
} else if (value instanceof Long) {
budle.putLong(key, ((Long) value).longValue());
} else if (value instanceof Float) {
budle.putFloat(key, ((Float) value).floatValue());
} else if (value instanceof Double) {
budle.putDouble(key, ((Double) value).doubleValue());
} else if (value instanceof Boolean) {
budle.putBoolean(key, ((Boolean) value).booleanValue());
} else if (value instanceof Character) {
budle.putChar(key, ((Character) value).charValue());
} else if (value instanceof JSONObject) {
budle.putBundle(key, fromJsonToBundle((JSONObject) value));
} else {
if (!value.getClass().isArray()) {
throw new IllegalArgumentException("Could not convert " + value.getClass());
}
fromArrayToBundle(budle, key, value);
}
}
}
return budle;
}
}
public static void put(Bundle bundle, String key, Object value) {
if (value instanceof Integer) {
bundle.putInt(key, (Integer) value);
} else if (value instanceof Float) {
bundle.putFloat(key, (Float) value);
} else if (value instanceof Character) {
bundle.putChar(key, (Character) value);
} else if (value instanceof CharSequence) {
bundle.putCharSequence(key, (CharSequence) value);
} else if (value instanceof Long) {
bundle.putLong(key, (Long) value);
} else if (value instanceof Short) {
bundle.putShort(key, (Short) value);
} else if (value instanceof Byte) {
bundle.putByte(key, (Byte) value);
} else if (value instanceof Boolean) {
bundle.putBoolean(key, (Boolean) value);
} else if (value instanceof Double) {
bundle.putDouble(key, (Double) value);
} else if (value instanceof Parcelable) {
bundle.putParcelable(key, (Parcelable) value);
} else if (value instanceof Bundle) {
bundle.putBundle(key, (Bundle) value);
} else if (value instanceof int[]) {
bundle.putIntArray(key, (int[]) value);
} else if (value instanceof byte[]) {
bundle.putByteArray(key, (byte[]) value);
} else if (value instanceof float[]) {
bundle.putFloatArray(key, (float[]) value);
} else if (value instanceof double[]) {
bundle.putDoubleArray(key, (double[]) value);
} else if (value instanceof boolean[]) {
bundle.putBooleanArray(key, (boolean[]) value);
} else if (value instanceof long[]) {
bundle.putLongArray(key, (long[]) value);
} else if (value instanceof Parcelable[]) {
bundle.putParcelableArray(key, (Parcelable[]) value);
} else if (value instanceof short[]) {
bundle.putShortArray(key, (short[]) value);
} else if (value instanceof String[]) {
bundle.putStringArray(key, (String[]) value);
} else {
// bundle.putString(key, String.valueOf(value));
}
}
public static void putValueToBundle(
@NonNull Bundle bundle, @NonNull String key, @NonNull Object value) {
if (value instanceof String) {
bundle.putString(key, (String) value);
} else if (value instanceof Integer) {
bundle.putInt(key, (int) value);
} else if (value instanceof Boolean) {
bundle.putBoolean(key, (boolean) value);
} else if (value instanceof Long) {
bundle.putLong(key, (long) value);
} else if (value instanceof Short) {
bundle.putShort(key, (short) value);
} else if (value instanceof Double) {
bundle.putDouble(key, (double) value);
} else if (value instanceof Float) {
bundle.putFloat(key, (float) value);
} else if (value instanceof Character) {
bundle.putChar(key, (char) value);
} else if (value instanceof Byte) {
bundle.putByte(key, (byte) value);
} else if (value instanceof CharSequence) {
bundle.putCharSequence(key, (CharSequence) value);
} else if (value instanceof Bundle) {
bundle.putBundle(key, (Bundle) value);
} else if (value instanceof Parcelable) {
bundle.putParcelable(key, (Parcelable) value);
} else if (value instanceof String[]) {
bundle.putStringArray(key, (String[]) value);
} else if (value instanceof int[]) {
bundle.putIntArray(key, (int[]) value);
} else if (value instanceof boolean[]) {
bundle.putBooleanArray(key, (boolean[]) value);
} else if (value instanceof long[]) {
bundle.putLongArray(key, (long[]) value);
} else if (value instanceof short[]) {
bundle.putShortArray(key, (short[]) value);
} else if (value instanceof double[]) {
bundle.putDoubleArray(key, (double[]) value);
} else if (value instanceof float[]) {
bundle.putFloatArray(key, (float[]) value);
} else if (value instanceof char[]) {
bundle.putCharArray(key, (char[]) value);
} else if (value instanceof byte[]) {
bundle.putByteArray(key, (byte[]) value);
} else if (value instanceof CharSequence[]) {
bundle.putCharSequenceArray(key, (CharSequence[]) value);
} else if (value instanceof Parcelable[]) {
bundle.putParcelableArray(key, (Parcelable[]) value);
} else if (value instanceof ArrayList) {
bundle.putIntegerArrayList(key, (ArrayList<Integer>) value);
} else if (value instanceof SparseArray) {
bundle.putSparseParcelableArray(key, (SparseArray<? extends Parcelable>) value);
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
if (value instanceof IBinder) {
bundle.putBinder(key, (IBinder) value);
return;
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (value instanceof Size) {
bundle.putSize(key, (Size) value);
return;
} else if (value instanceof SizeF) {
bundle.putSizeF(key, (SizeF) value);
return;
}
}
if (value instanceof Serializable) {
bundle.putSerializable(key, (Serializable) value);
return;
}
throw new RuntimeException(String.format(Locale.getDefault(),
"Arguments extra %s has wrong type %s.", key, value.getClass().getName()));
}
}
@SuppressWarnings("unchecked")
private static void setBundleValue(@NonNull Field field, @NonNull Object obj,
@NonNull Bundle bundle, @NonNull String key, boolean isGson)
throws IllegalAccessException {
if (isGson) {
bundle.putString(key, GsonHelper.toJson(field.get(obj)));
return;
}
Class<?> type = field.getType();
Type[] genericTypes = null;
if (field.getGenericType() instanceof ParameterizedType) {
genericTypes = ((ParameterizedType) field.getGenericType()).getActualTypeArguments();
}
if (type.equals(Boolean.TYPE)) {
bundle.putBoolean(key, field.getBoolean(obj));
} else if (type.equals(boolean[].class)) {
bundle.putBooleanArray(key, (boolean[]) field.get(obj));
} else if (type.equals(Bundle.class)) {
bundle.putBundle(key, (Bundle) field.get(obj));
} else if (type.equals(Byte.TYPE)) {
bundle.putByte(key, field.getByte(obj));
} else if (type.equals(byte[].class)) {
bundle.putByteArray(key, (byte[]) field.get(obj));
} else if (type.equals(Character.TYPE)) {
bundle.putChar(key, field.getChar(obj));
} else if (type.equals(char[].class)) {
bundle.putCharArray(key, (char[]) field.get(obj));
} else if (type.equals(CharSequence.class)) {
bundle.putCharSequence(key, (CharSequence) field.get(obj));
} else if (type.equals(CharSequence[].class)) {
bundle.putCharSequenceArray(key, (CharSequence[]) field.get(obj));
} else if (type.equals(Double.TYPE)) {
bundle.putDouble(key, field.getDouble(obj));
} else if (type.equals(double[].class)) {
bundle.putDoubleArray(key, (double[]) field.get(obj));
} else if (type.equals(Float.TYPE)) {
bundle.putFloat(key, field.getFloat(obj));
} else if (type.equals(float[].class)) {
bundle.putFloatArray(key, (float[]) field.get(obj));
} else if (type.equals(Integer.TYPE)) {
bundle.putInt(key, field.getInt(obj));
} else if (type.equals(int[].class)) {
bundle.putIntArray(key, (int[]) field.get(obj));
} else if (type.equals(Long.TYPE)) {
bundle.putLong(key, field.getLong(obj));
} else if (type.equals(long[].class)) {
bundle.putLongArray(key, (long[]) field.get(obj));
} else if (type.equals(Short.TYPE)) {
bundle.putShort(key, field.getShort(obj));
} else if (type.equals(short[].class)) {
bundle.putShortArray(key, (short[]) field.get(obj));
} else if (type.equals(String.class)) {
bundle.putString(key, (String) field.get(obj));
} else if (type.equals(String[].class)) {
bundle.putStringArray(key, (String[]) field.get(obj));
} else if (Parcelable.class.isAssignableFrom(type)) {
bundle.putParcelable(key, (Parcelable) field.get(obj));
} else if (type.equals(ArrayList.class)
&& genericTypes != null
&& genericTypes[0] instanceof Class
&& Parcelable.class.isAssignableFrom((Class<?>) genericTypes[0])) {
bundle.putParcelableArrayList(key, (ArrayList<? extends Parcelable>) field.get(obj));
} else if (type.isArray() && Parcelable.class.isAssignableFrom(type.getComponentType())) {
bundle.putParcelableArray(key, (Parcelable[]) field.get(obj));
} else if (Serializable.class.isAssignableFrom(type)) {
bundle.putSerializable(key, (Serializable) field.get(obj));
} else {
throw new RuntimeException("Unsupported field type: " + field.getName()
+ ", " + type.getName());
}
}
@Override
public void setBundleValue(Bundle bundle, String key, Cursor cursor, int columnIndex) {
bundle.putShort(key, cursor.getShort(columnIndex));
}
public Intent getTag(int serviceHandle, int slotNumber, byte[] atr, byte[] hiLayer, byte[] id, boolean hce, byte[] historicalBytes, INfcTag tagService) {
/*
if (id != null && id[0] != NXP_MANUFACTURER_ID) {
throw new IllegalArgumentException("Non-NXP tag id");
}
*/
List<Bundle> bundles = new ArrayList<Bundle>();
List<Integer> tech = new ArrayList<Integer>();
Bundle nfcA = new Bundle();
nfcA.putShort(MifareUltralightTagFactory.EXTRA_SAK, EXTRA_SAK_VALUE);
nfcA.putByteArray(MifareUltralightTagFactory.EXTRA_ATQA, EXTRA_ATQA_VALUE);
bundles.add(nfcA);
tech.add(TagTechnology.NFC_A);
Bundle desfire = new Bundle();
desfire.putByteArray(EXTRA_HIST_BYTES, historicalBytes);
if(hiLayer != null) {
desfire.putByteArray(EXTRA_HI_LAYER_RESP, hiLayer);
}
bundles.add(desfire);
tech.add(TagTechnology.ISO_DEP);
final Intent intent = new Intent(NfcTag.ACTION_TAG_DISCOVERED);
int[] techArray = new int[tech.size()];
for (int i = 0; i < techArray.length; i++) {
techArray[i] = tech.get(i);
}
intent.putExtra(NfcAdapter.EXTRA_TAG, createTag(id, techArray, bundles.toArray(new Bundle[bundles.size()]), serviceHandle, tagService));
if (id != null) {
intent.putExtra(NfcAdapter.EXTRA_ID, id);
}
intent.putExtra(NfcTag.EXTRA_TAG_SERVICE_HANDLE, serviceHandle);
intent.putExtra(NfcTag.EXTRA_TAG_SLOT_NUMBER, slotNumber);
intent.putExtra(NfcTag.EXTRA_HOST_CARD_EMULATION, hce);
return intent;
}
private static void putShort(String key, Bundle bundle) {
bundle.putShort(key, (short)random.nextInt());
}
/**
* 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 Short fieldValue) {
state.putShort(key, fieldValue);
}