"Idiot brother and you strengthen the application layer" read Develop API Guides think (one)

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 number that our app is compatible with, that is, our app does not work properly under this version number, possibly due to the use of a higher version number of the API or theme, anyway, it is not normal execution.

The Android:targetsdkversion property specifies the highest version number that is optimized by us. This property does not guarantee that our app will not execute on a higher version number. It is used to indicate whether our app should inherit changes in behavior that occur on a higher version number. Let's say we don't have an update: Targetsdkversion to the latest version number, then the latest version number is executed. Then the system assumes that our app needs to be backwards compatible.

For example, the behavior of Alarmmanager has changed in the 4.4 version number. In order to conserve power, the system will alarm the same time difference, so that your alarm is not guaranteed to execute on time. So, assuming that your targetsdkversion is greater than or equal to 19, then the execution time is indeterminate. However, if it is less than 19, then the old API will be used. Ensure that alarm can be executed on time.

Advanced knowledge about Onsaveinstancestate ()

We all know that assuming the implementation of onsaveinstancestate (), the ability to store some of the interface in the state, so that when the UI is restored, you may use the saved values to restore the UI scene.

In fact, the default onsaveinstancestate () implementation saves the state of the view with the id attribute in the current interface for on-site recovery. Almost all of the controls in the Android Framework framework implement the Onsaveinstancestate () method appropriately, for example EditText saves the text. Checkboxes are saved in the selected state, and so on.

So, suppose you want to restore the scene, set an ID property on the controls in the layout OK ~

Of course, we are also able to control the view not to save its own state. Just set android:saveenabled to False or call setsaveenabled () to be able to

Other than that. If you want to test your activity to save the user's data, you just need to change your screen orientation to test it. When the screen orientation has changed. Your activity will be destory and then the information on the create,activity should not be lost, otherwise, change your code.

In fact, the reason that the screen orientation changes, the activity will be generated again, because at the time of execution. The configurations of the device has changed, in order for our app to be able to sense such changes and make the right response. The system will load the current activity again, which includes changes in screen orientation, changes in language environment, changes in keyboard status, and so on. And the android:configchanges that we set under the Activity tab are exactly the same.

About StartActivity and intent

We use intent to start an activity that is simply a matter of simple, but suppose you want to send an implicit intention to open activity. You need to consider situations where there is no activity matching your intent action, as 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();}

Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

"Idiot brother and you strengthen the application layer" read Develop API Guides think (one)

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.