Comparison between the android SDK version and version name and a programming tips

Source: Internet
Author: User

A few days ago, in order to solve a problem, we reversed a small tool software. I have found some useful things in this process. I would like to share with you here. First, declare a few points: 1. The reverse Code requires a lot of manual analysis. I only read a little bit. 2. The Code will not be made public. Please do not ask me for the code, I am afraid of trouble ~~~ Alas, I am sorry for this bad thing ~~~ ~~~ Ah!

1. Implement the GPS switch through programming

On the Android mobile phone and simulator, there is a configuration interface for managing the GPS switch, as shown below:

Through this management interface, we can manage GPS to implement our mobile phone positioning function. I used to collect information on the Internet, hope to find the source code for GPS management through code, and I did find a piece of code (I will attach the code later ), however, this code cannot run in SDK 2.3 or later versions. As a result, an evil idea is generated ~

After analyzing the tool mentioned above, we found its function module for GPS management. After a simple analysis, we found that when the android SDK version is different, the GPS control code is also different. First, the GPS control code in version 2.2 and earlier is attached:

/**
* A gps switch is implemented. If the current switch is disabled, the switch is enabled. If the current switch is enabled, the switch is disabled.
* Applicable version:
* 1.6/2.1/2.2
*
* The enumerated values of Number 3 in URI. parse () parameter "custom: 3" are as follows:
* Private Static final int button_bluetooth = 4;
* Private Static final int button_brightness = 1;
* Private Static final int button_gps = 3;
* Private Static final int button_sync = 2;
* Private Static final int button_wifi = 0;
*
*/
Private void togglegps (){
// When the SDK version is earlier than 2.3
If (build. version. sdk_int <build. version_codes.gingerbread ){
Intent gpsintent = new intent ();
Gpsintent. setclassname ("com. Android. Settings ",
"Com. Android. settings. widget. settingsappwidgetprovider ");
Gpsintent. addcategory ("android. Intent. Category. Alternative ");
Gpsintent. setdata (URI. parse ("custom: 3 "));
Try {
Pendingintent. getbroadcast (this, 0, gpsintent, 0). Send ();
} Catch (canceledexception e ){
E. printstacktrace ();
}
}
}

The above Code uses the widget plug-in provided by the Android platform to manage various switches. A gps switch is implemented. If the current GPS is off, turn it on; if the GPS is on, turn it off.

You may have noticed that the first line in the function body comments "when the SDK version is 2.3 or lower". We have made a judgment on the SDK version here. This judgment is also the content that we will focus on in the next section. We will put it aside for the time being. Don't be biased in this section.

In SDK 2.3 and later versions, the tool uses a static method of settings. secure in the SDK class:

Public static final void setlocationproviderenabled (contentresolver Cr, string provider, Boolean enabled)

Since: API Level 8

Thread-safe method for enabling or disabling a single location provider.

Parameters

Cr

The content resolver to use

Provider

The location provider to enable or disable

Enabled

True if the provider shocould be enabled

This method is provided from API level 8. the SDK version corresponding to API Level 8 is 2.2, OK! Normally, this function should support sdk2.3. Write a function. (This code is relatively simple, so I will not post the code again.) The result is surprising, but the user is not assigned the permission "android. permission. write_settings "; well, add the permission, and prompt that the permission" android. permission. write_secure_settings. Next, we will see the sad reminder that we do not have the permission to "android. Permission. write_secure_settings. Why do I still prompt if I already have permissions added. Finally, I also searched for materials in various forums, saying that Google had completely locked this permission in Version 2.3. Well, it's a tragedy, unless you change the android code yourself, otherwise, there will be no other way.

So, at the end of section 1, I would like to remind you that if you want to manage GPS in sdk2.3, you should use your intent to open the system's default GPS-managed activity.

Ii. SDK version Comparison

In order to download the android SDK source code, I looked for a link everywhere and found that there was something similar to the English name behind the file name?

Indeed, beginners like me do not know what this English word means. However, in the process of inverse code, we found that the android API provided such a class android. OS. build: This class defines the version number, version name, and other information of each android SDK version. If you are interested, go to the development documentation.

The English word here is the version name of each SDK version.

Version_codes

    /**
* Enumeration of the currently known SDK version codes. These are the
* values that can be found in {@link VERSION#SDK}. Version numbers
* increment monotonically with each official platform release.
*/
public static class VERSION_CODES {
/**
* Magic version number for a current development build, which has
* not yet turned into an official release.
*/
public static final int CUR_DEVELOPMENT = 10000;

/**
* October 2008: The original, first, version of Android. Yay!
*/
public static final int BASE = 1;

/**
* February 2009: First Android update, officially called 1.1.
*/
public static final int BASE_1_1 = 2;

/**
* May 2009: Android 1.5.
*/
public static final int CUPCAKE = 3;

/**
* September 2009: Android 1.6.
*
* <p>Applications targeting this or a later release will get these
* new changes in behavior:</p>
* <ul>
* <li> They must explicitly request the
* {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission to be
* able to modify the contents of the SD card. (Apps targeting
* earlier versions will always request the permission.)
* <li> They must explicitly request the
* {@link android.Manifest.permission#READ_PHONE_STATE} permission to be
* able to be able to retrieve phone state info. (Apps targeting
* earlier versions will always request the permission.)
* <li> They are assumed to support different screen densities and
* sizes. (Apps targeting earlier versions are assumed to only support
* medium density normal size screens unless otherwise indicated).
* They can still explicitly specify screen support either way with the
* supports-screens manifest tag.
* </ul>
*/
public static final int DONUT = 4;

/**
* November 2009: Android 2.0
*
* <p>Applications targeting this or a later release will get these
* new changes in behavior:</p>
* <ul>
* <li> The {@link android.app.Service#onStartCommand
* Service.onStartCommand} function will return the new
* {@link android.app.Service#START_STICKY} behavior instead of the
* old compatibility {@link android.app.Service#START_STICKY_COMPATIBILITY}.
* <li> The {@link android.app.Activity} class will now execute back
* key presses on the key up instead of key down, to be able to detect
* canceled presses from virtual keys.
* <li> The {@link android.widget.TabWidget} class will use a new color scheme
* for tabs. In the new scheme, the foreground tab has a medium gray background
* the background tabs have a dark gray background.
* </ul>
*/
public static final int ECLAIR = 5;

/**
* December 2009: Android 2.0.1
*/
public static final int ECLAIR_0_1 = 6;

/**
* January 2010: Android 2.1
*/
public static final int ECLAIR_MR1 = 7;

/**
* June 2010: Android 2.2
*/
public static final int FROYO = 8;

/**
* Newest version of Android, version 2.3.
*/
public static final int GINGERBREAD = 9;
}

Android 1.5: Cupcake (cup cake)
Android 1.6: donut (donut)
Android 2.0/2.1: eclair (lightning puff)
Android 2.2: froyo (frozen)
Android 2.3: gingerbread (ginger pie)
Android 3.0: honeycomb (honeycomb)

Sometimes, in the coding process, we may encounter different implementation schemes for a specific function under different SDK versions. In this case, we need to determine the SDK version and adopt different implementation schemes, in this way, our app can ensure good compatibility. Therefore, we believe that the following simple judgment statement is very useful to you:

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
......
}

If you want to have a more in-depth understanding of this, it is recommended to take a closer look at the class android. OS. Build, the development documentation always makes us learn the weapon.

I hope these things will be useful to everyone! I would like to share with you!

Related Article

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.