1, if return模式+log输出的写法,例子:
if(s=1) 输出日志 return 结束。
2, switch筛选合格的参数,然后通过default来抛出异常,典型例子为:
switch (direction) {
case AudioManager.ADJUST_LOWER:
case AudioManager.ADJUST_RAISE:
case AudioManager.ADJUST_SAME:
case AudioManager.ADJUST_MUTE:
case AudioManager.ADJUST_UNMUTE:
case AudioManager.ADJUST_TOGGLE_MUTE:
break;
default:
throw new IllegalArgumentException("Bad direction " + direction);
}
3, 对于抛出异常的应用:
private void ensureValidStreamType(int streamType) {
if (streamType < 0 || streamType >= mStreamStates.length) {
throw new IllegalArgumentException("Bad stream type " + streamType);
}
}
4, final类型的数组应用,定义一串连续的参数,使用数组组合的方式。
public static final String[] STREAM_NAMES = new String[] {
“STREAM_VOICE_CALL”,
“STREAM_SYSTEM”,
“STREAM_RING”,
“STREAM_MUSIC”,
“STREAM_ALARM”,
“STREAM_NOTIFICATION”,
“STREAM_BLUETOOTH_SCO”,
“STREAM_SYSTEM_ENFORCED”,
“STREAM_DTMF”,
“STREAM_TTS”,
“STREAM_ACCESSIBILITY”
};
public static int[] DEFAULT_STREAM_VOLUME = new int[] {
4, // STREAM_VOICE_CALL
7, // STREAM_SYSTEM
5, // STREAM_RING
5, // STREAM_MUSIC
6, // STREAM_ALARM
5, // STREAM_NOTIFICATION
7, // STREAM_BLUETOOTH_SCO
7, // STREAM_SYSTEM_ENFORCED
5, // STREAM_DTMF
5, // STREAM_TTS
5, // STREAM_ACCESSIBILITY
};
5,handle配合用法。
通过定义对应的常量来控制key。
然后通过handleMessage来转发
switch (msg.what) {
case MSG_SET_DEVICE_VOLUME:
setDeviceVolume((VolumeStreamState) msg.obj, msg.arg1);
break;
case MSG_SET_ALL_VOLUMES:
setAllVolumes((VolumeStreamState) msg.obj);
break;
case MSG_PERSIST_VOLUME:
persistVolume((VolumeStreamState) msg.obj, msg.arg1);
break;
case MSG_PERSIST_RINGER_MODE:
// note that the value persisted is the current ringer mode, not the
// value of ringer mode as of the time the request was made to persist
persistRingerMode(getRingerModeInternal());
break;