[Kaizige takes you to consolidate the application layer] Read Develop API Guides (I), developguides
Reprinted please indicate the source: http://blog.csdn.net/zhaokaiqiang1992
-
- Dynamic Detection of device Properties
- Definition of targetSdkVersion
- Advanced onSaveInstanceState knowledge
- StartActivity and Intent
Dynamic Detection of device Properties
We can use the following code to dynamically check whether the current device has a certain feature, such as whether there is a direction sensor.
PackageManager pm = getPackageManager();if(!pm.hasSystemFeature(PackageManager.FEATURE_SENSOR_COMPASS)) { // This device does not have a compass, turn off the compass feature disableCompassFeature();}
Definition of targetSdkVersion
We often set this in the configuration file.
<manifest ... > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" /> ...</manifest>
Android: minSdkVersion refers to the lowest version compatible with our App, that is, our App cannot run normally under this version, probably because it uses a later version of API or Theme, it cannot run normally.
The android: targetSdkVersion attribute specifies the most optimized version. This attribute does not ensure that our App cannot run on a higher version, but is used to show whether our App should inherit the behavior changes in a later version. If we haven't updated: targetSdkVersion to the latest version, then when running on the latest version, the system assumes that our App needs to be backward compatible.
For example, in version 4.4, the behavior of AlarmManager has changed. In order to save power, the system puts Alarm with the same time for execution, in this way, you cannot ensure that your Alarm runs on time. Therefore, if your targetSdkVersion is greater than or equal to 19, the running time is uncertain, but if it is less than 19, the old API will be used to ensure that Alarm can run on time.
Advanced knowledge about onSaveInstanceState ()
We all know that if onSaveInstanceState () is implemented, some interface states can be saved in it, so that when the UI is restored, the saved values may be used to restore the UI site.
In fact, the default onSaveInstanceState () implementation will save the status of all views with the id attribute on the current interface for on-site recovery. In the framework of Android, almost all controls properly implement the onSaveInstanceState () method. For example, EditText stores the text and CheckBox stores the selected state. Therefore, if you want to restore the scene, set an id attribute for all controls in the layout ~
Of course, you can also control the View status by yourself, as long as you set android: saveEnabled to false or call setSaveEnabled ().
In addition, if you want to test whether your Activity can properly store user data, you only need to change the direction of your screen to test. When the screen direction changes, your Activity will be Destory and then Create. The information on the Activity should not be lost. Otherwise, modify your code!
Actually, the Activity will be re-generated when the screen direction changes, because the device's deployments have changed during the operation. In order to allow our App to detect such changes and make correct responses, the system will reload the current Activity, including screen orientation, language environment, and keyboard status, which is exactly the same as android: configChanges we set under the Activity tag.
StartActivity and Intent
We can use Intent to start an Activity. However, if you want to send an implicit Intent to enable the Activity, you need to consider the situation where no Activity matches your Intent Action, because this will cause the program crash. Therefore, when sending implicit intent in the future, use the following form.
Intent intent = new Intent(AlarmClock.ACTION_SET_TIMER) .putExtra(AlarmClock.EXTRA_MESSAGE, "Timer'") .putExtra(AlarmClock.EXTRA_LENGTH, 60) .putExtra(AlarmClock.EXTRA_SKIP_UI, true); if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } else { Toast.makeText(this, "No Activity", Toast.LENGTH_SHORT).show(); }