Android怎样存取用户偏好设置

admin 提交于 星期二, 02/10/2015 - 15:34

SharedPreferences prefs = this.getSharedPreferences(
      "com.example.app", Context.MODE_PRIVATE);

读取设置:

String dateTimeKey = "com.example.app.datetime";

// use a default value using new Date()
long l = prefs.getLong(dateTimeKey, new Date().getTime()); 

编辑保存设置:

Date dt = getSomeDate();
prefs.edit().putLong(dateTimeKey, dt.getTime()).apply();

标签

Android怎样检查当前是否联网

admin 提交于 星期二, 02/10/2015 - 15:21

public boolean isOnline() {
    ConnectivityManager cm =
        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    return netInfo != null && netInfo.isConnectedOrConnecting();
}

在AndroidManifest.xml中添加下面权限:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

标签

Android怎样获取以像素为单位的屏幕尺寸

admin 提交于 星期二, 02/10/2015 - 11:37

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

如果不在Activity里:

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();

标签