AndroidSDK版本号与版本名称对照及⼀个编程⼩技巧
VERSION_CODES
前⼏天,为了解决⼀个问题,逆了⼀个⼩⼯具软件。在这个过程发现了⼀些很有⽤的东西,在此跟⼤家分享⼀下。⾸先声明⼏点:1、逆出来的代码,需要⼤量的⼈⼯分析,我现在只看了⼀点点;2、代码不会公开,请不要向我要代码,我怕⿇烦~~~唉,做了这件坏事,内⼼忐~~~忑~~~啊!
⼀、通过编程实现对GPS的开关
  在Android⼿机和模拟器上有⼀个管理GPS开关的设置界⾯,如下:
  通过这个管理界⾯,我们可以对GPS进⾏管理,以实现我们的⼿机定位功能。曾经在⽹上搜集资料,希望到通过代码对GPS进⾏管理的源码,也确实到了⼀份代码(稍后附上代码),但是这份代码在SDK 2.3及以上的版本中,始终不能运⾏。于是,产⽣了⼀个逆向的邪念~
  在分析上⾯提到的⼩⼯具之后,发现了其对GPS管理的功能模块,经过简单的分析之后,发现在Android SDK版本不同的情况下,对GPS控制的代码还不⼀样。⾸先附上在2.2版本及以前版本的GPS控制代码:
/**
* 实现了⼀个GPS的开关,当前关闭则打开,当前打开则关闭
* 适⽤版本号:
* 1.6 / 2.1 / 2.2
*
* Uri.parse()的参数"custom:3"中的数字3的枚举值如下:
* 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() {
//
当SDK版本号为2.3以下版本时
if
(Build.VERSION.SDK_INT
<
Build.VERSION_CODES.GINGERBREAD) {
Intent gpsIntent
=
new
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
{
this
,
, gpsIntent,
).send();
}
catch
(CanceledException e) {
e.printStackTrace();
}
}
}
  以上这段代码,是利⽤Android平台⾃带的Widget插件对各种开关进⾏管理的。实现的是⼀个GPS的开关,如果当前GPS处于关闭状态,则将其打开;如果GPS处于打开状态,则将其关闭。
  ⼤家可能已经注意到函数体内部的第⼀⾏注释“当SDK版本号为2.3⼀下版本时”,的确我们在这⾥做了⼀个针对SDK版本的判断。这⼀个判断也是我们下⼀个⼩节要重点介绍的内容,暂时放在⼀边,不要在这⼀节⾥⾯跑偏了。
  在SDK 2.3及之后的版本中,那个⼩⼯具利⽤了SDK中的类Settings.Secure的⼀个静态⽅法:
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 should be enabled
  这个⽅法从API Level 8才开始提供,API Level 8对应的SDK版本是2.2,OK!那按照正常情况来说,这个函数应该是⽀持SDK2.3的。不妨写个函数来试试。(这个代码⽐较简单,我就不再贴代码了)结果却令⼈意外,没有给⽤户分配权
限"android.permission.WRITE_SETTINGS";好嘛,那就加上权限;⼜提⽰没有权
限“android.permission.WRITE_SECURE_SETTINGS”,好说,再加上这个权限。接下来,见证悲催的时刻到了,还是提⽰没
有“android.permission.WRITE_SECURE_SETTINGS”的权限。明明已经加上了权限,为何还是提⽰。
最后也是在各种⼤⼩论坛⾥⾯查资料,说是在2.3版本⾥⾯,Google把这个权限完全锁住了,好吧,悲剧了,除⾮你⾃⼰改Android代码,否则就真的没有别的办法了。
  所以,在第⼀节结束的时候,提醒各位童鞋,如果想在SDK2.3版本管理GPS,还是乖乖的⽤你的Intent打开系统默认的管理GPS的Activity吧。
⼆、SDK版本对照
  为了下载android SDK的源码,我到处链接,好不容易⼀个链接,发现⽂件名后⾯怎么还有⼀个类似英⽂名的东西?
  的确,像我这样的初学者,是不知道这个英⽂单词代表什么意思的。但是在逆代码的过程,发现了Android API给我们提供了这样个类android.os.Build,在这个类中定义了Android SDK每个版本的版本号,版本名,以后其他⼀些信息,感兴趣的同学可以去开发⽂档中看看。
  这⾥的英⽂单词,就是每个SDK版本的版本名称。
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
=
android编程入门指南 pdf2
;
/**
* 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
=

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。