Cocoa应用程序执行流程

Alt none

Android Preference默认值设置

在XML文件(advanced_preferences.xml)中为Preference设置默认值

<!-- default value is a boolean -->
<CheckBoxPreference
    android:defaultValue="true"
    ... />

<!-- default value is a string -->
<ListPreference
    android:defaultValue="@string/pref_syncConnectionTypes_default"
    ... />

在代码中还需要调用

PreferenceManager.setDefaultValues(this, R.xml.advanced_preferences, false);

第三个参数接受一个BOOL值,含义如下:

true:每次调用setDefaultValues都会导致Preference的初始化。
false:只初始化一次,重复调用setDefaultValues不会导致重复初始化。

PreferenceManager.setDefaultValues() This method takes three arguments:

  • Your application Context.

  • The resource ID for the preference XML file for which you want to set the default values.

  • A boolean indicating whether the default values should be set more than once.

  • When false, the system sets the default values only if this method has never been called in the past (or the KEY_HAS_SET_DEFAULT_VALUES in the default value shared preferences file is false).

As long as you set the third argument to false, you can safely call this method every time your activity starts without overriding the user’s saved preferences by resetting them to the defaults. However, if you set it to true, you will override any previous values with the defaults.

Android Battery Information BroadcastReceiver

public class MainActivity extends Activity {
    private static final String TAG = "TestBatteryInfo";
    private TextView batteryInfo;
    private ImageView imageBatteryState;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        batteryInfo = (TextView) findViewById(R.id.textViewBatteryInfo);
        imageBatteryState = (ImageView) findViewById(R.id.imageViewBatteryState);

        IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(batteryInfoReceiver, filter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(batteryInfoReceiver);
    }

    private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d(TAG, "Get Battery Broadcast: " + intent.toString());
            Toast.makeText(MainActivity.this,
                    "Get Battery Broadcast: " + intent.toString(),
                    Toast.LENGTH_SHORT).show();
            int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, 0);
            int icon_small = intent.getIntExtra(
                    BatteryManager.EXTRA_ICON_SMALL, 0);
            int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
            int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
            boolean present = intent.getExtras().getBoolean(
                    BatteryManager.EXTRA_PRESENT);
            int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
            int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, 0);
            String technology = intent.getExtras().getString(
                    BatteryManager.EXTRA_TECHNOLOGY);
            int temperature = intent.getIntExtra(
                    BatteryManager.EXTRA_TEMPERATURE, 0);
            int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0);

            batteryInfo.setText("Health: " + health + "\n" + "Icon Small:"
                    + icon_small + "\n" + "Level: " + level + "\n"
                    + "Plugged: " + plugged + "\n" + "Present: " + present
                    + "\n" + "Scale: " + scale + "\n" + "Status: " + status
                    + "\n" + "Technology: " + technology + "\n"
                    + "Temperature: " + temperature + "\n" + "Voltage: "
                    + voltage + "\n");
            imageBatteryState.setImageResource(icon_small);
        }
    };
}

Get physical screen size

public void getRealSize (Point outSize)

Added in API level 17 Gets the real size of the display without subtracting any window decor or applying any compatibility scale factors.

The size is adjusted based on the current rotation of the display.

The real size may be smaller than the physical size of the screen when the window manager is emulating a smaller display (using adb shell am display-size).

Parameters outSize Set to the real size of the display.

//API > 17
WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point point = new Point();
display.getRealSize(point);

PHP 正则元字符

php正则元字符-1

php正则元字符-1

php正则元字符-2

php正则元字符-2

php正则元字符-3

php正则元字符-3

正则元字符-4

正则元字符-4