"Kyle, take you to tamp down the application layer" read Develop API Guides feelings (i.)

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/zhaokaiqiang1992

      • Dynamic detection of device properties
      • About the meaning of targetsdkversion
      • Advanced knowledge about Onsaveinstancestate
      • About StartActivity and intent

Dynamic detection of device properties

We can use the following code to dynamically detect whether the current device has a certain feature, such as whether there is a directional sensor

PackageManager pm = getPackageManager();if(!pm.hasSystemFeature(PackageManager.FEATURE_SENSOR_COMPASS)) {    //Thisnot have a compass, turn off the compass feature    disableCompassFeature();}
About the meaning of targetsdkversion

We often set this in the manifest file

... >    <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" />    ...</manifest>

Android:minsdkversion is the minimum version that our app is compatible with, that is, our app doesn't work properly under this version, probably because a later version of the API or theme is not working anyway.

The Android:targetsdkversion property specifies the highest version that is optimized by us. This property does not guarantee that our app will not run on a higher version, but rather to indicate whether our app should inherit changes in behavior that occur on a later version. If we don't have an update: Targetsdkversion to the latest version, then when running on the latest version, then the system assumes that our app needs to be backwards compatible.

For example, in version 4.4, the behavior of Alarmmanager has changed, in order to save power, the system will be a little time difference alarm put together to execute, so there is no guarantee that your alarm run on time. So, if your targetsdkversion is greater than or equal to 19, then the running time is indeterminate, but if it is less than 19, then the old API will be used to ensure that alarm can run on time.

Advanced knowledge about Onsaveinstancestate ()

We all know that if you implement Onsaveinstancestate (), you can save the state of some interfaces inside, so that when the UI resumes, you might be able to restore the UI site with the saved values.

In fact, the default onsaveinstancestate () implementation will save the state of all view with the id attribute on the current interface for on-site recovery. Almost all of the controls in the Android Framework framework implement the Onsaveinstancestate () method appropriately, such as EditText saves the text, the checkbox saves the selected state, and so on. So, if you want to restore the scene, set an ID property on the control in the layout OK ~

Of course, we can also control the view not to save its own state, as long as the set android:saveenabled to False or call setsaveenabled () can be

In addition, if you want to test your activity can be normal to save user data, you just need to change your screen orientation can be tested, when the screen direction changes, your activity will be destory and then create,activity on the information should not be lost , otherwise, change your code!

In fact, the reason why the screen orientation changes, activity will regenerate, because at run time, the device's configurations has changed, in order to let our app can sense this change and make the correct response, the system will reload the current activity, This change includes changes in screen orientation, changes in the language environment, changes in the keyboard state, and so on, and the android:configchanges that we set under the Activity tab are exactly the same.

About StartActivity and intent

It's easy for us to use intent to start an activity, but if you want to send an implicit intention to start the activity, you need to consider the case that there is no activity matching your intent action, Because this will cause the program to crash. So, in the future, when sending implicit intent, use the following form.

Intent Intent = new Intent (alarmclock. ACTION_set_timer). PutExtra(AlarmClock. EXTRA_message,"Timer "). PutExtra(AlarmClock. EXTRA_length, -). PutExtra(AlarmClock. EXTRA_SKIP_UI, True);if (intent. Resolveactivity(Getpackagemanager ()) = null) {startactivity (Intent);} else {Toast. Maketext(This,"No Activity", Toast. LENGTH_short). Show();}

"Kyle, take you to tamp down the application layer" read Develop API Guides feelings (i.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.