以上面代码为例
a.调用名称为CODE_PASSWORD_ACTIVITY
b.右侧值为数值,第一个为0,余下的的为1<
// 自定义RequestCode.CODE_PASSWORD_ACTIVITY
interface RequestCode {
public static final int CODE_PASSWORD_ACTIVITY = 0;
public static final int CODE_SETTING_ACTIVITY = 1<<0;
public static final int CODE_FAVORITE_ACTIVITY = 1<<1;
}
以上参考Objective-C 中枚举类型 和 Android中 常量命名规则
// enum {
// UIControlStateNormal = 0, 常规状态显现
// UIControlStateHighlighted = 1 << 0, 高亮状态显现
// UIControlStateDisabled = 1 << 1, 禁用的状态才会显现
// UIControlStateSelected = 1 << 2, 选中状态
// UIControlStateApplication = 0x00FF0000, 当应用程序标志时
// UIControlStateReserved = 0xFF000000 为内部框架预留,可以不管他
// };
/**
* Standard constants and tools for placing an object within a potentially
* larger container.
*/
public class Gravity
{
/** Constant indicating that no gravity has been set **/
public static final int NO_GRAVITY = 0x0000;
...
/** Place object in the vertical center of its container, not changing its
* size. */
public static final int CENTER_VERTICAL = AXIS_SPECIFIED<
8.Android项目中Log输出,类别分verbose、debug、info、warn、error、assert, 以下是个人习惯,仅供参考
过滤级别为verbose
public class WelcomeActivity {
private static final String TAG = WelcomeActivity.class.getSimpleName(); // 建议
// private static final String TAG = “WelcomeActivity”; // 不建议,一旦重命名类名,类名不会自动改变,一不注意会影响后期调试
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "onCreate :: savedInstanceState = " + savedInstanceState);
// 因为是信息级别,所以比较喜欢用Log.i, 输出内容是参照C#中Log输出编写,具体格式为:方法名 + “ :: ”(::左右有空格) + 变量名 + “ = ”(左右有空格) + 变量
super.onCreate(savedInstanceState);
}
@Override
protected void onResume() {
Log.i(TAG, "onResume ::");
super.onResume();
}
/**
* Add shortcut to launcher (多写注释和描述方法,易于代码维护和阅读,具体格式可以参考Java的文档规范)
*/
private void addShortcut() {
Log.i(TAG, "addShortcut ::");
// 信息级别的输出,所以喜欢用Log.i
Intent intent = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT");
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
getString(R.string.app_name));
intent.putExtra("duplicate", false);
ComponentName componentName = new ComponentName(getPackageName(),
getPackageName() + "." + getLocalClassName());
Log.d(TAG, "addShortcut :: componentName = " + componentName);
// 调试级别的输出,所以用Log.d
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(
Intent.ACTION_MAIN).setComponent(componentName));
ShortcutIconResource iconResource = Intent.ShortcutIconResource
.fromContext(context, R.drawable.ic_launcher);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
sendBroadcast(intent);
}
/**
* Judge have the shortcut on launcher or not
* (多写注释和描述方法,易于代码维护和阅读,具体格式可以参考Java的文档规范)
* @return ...
*/
private boolean hasShortcut() {
Log.i(TAG, "hasShortcut ::");
String url;
if (Build.VERSION.SDK_INT < 8) {
url = "content://com.android.launcher.settings/favorites notify=true";
} else {
url = "content://com.android.launcher2.settings/favorites notify=true";
}
Cursor cursor = null;
try {
ContentResolver resolver = getContentResolver();
cursor = resolver.query(Uri.parse(url), new String[] { "title" },
"title= ", new String[] { getString(R.string.app_name) },
null);
if (cursor != null && cursor.getCount() > 0) {
cursor.close();
return true;
}
} catch (Exception e) {
Log.e(TAG, "hasShortcut :: Exception => Error: " + e.getMessage());
// 错误级别的输出,所以用Log.e
e.printStackTrace();
// 输出错误栈堆里的信息,方便调试
} finally {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
return false;
}
/**
* Get the section tags like 'Hot tag' or 'search history'
* (多写注释和描述方法,易于代码维护和阅读,具体格式可以参考Java的文档规范)
* @param groups
* @return
*/
private List
getGroups(String[] groups) {
Log.i(TAG, "getGroups :: groups = " + groups)