/***************************************************

*TODO: description .

* @author: gao_chun

* @since: 2015-4-1

* @version: 1.0.0

*@remark: 转载请注明出处

**************************************************/


Froyo(android 2.2,API Level:8)中引入了android:installLocation

通过设置该属性可以使得开发者以及用户决定程序的安装位置.

android:installLocation属于AndroidManifest.XML中的manifest节点.如下所示:

[html] view plain copy
  1. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
  2. package="string"
  3. android:sharedUserId="string"
  4. android:sharedUserLabel="stringresource"
  5. android:versionCode="integer"
  6. android:versionName="string"
  7. android:installLocation=["auto"|"internalOnly"|"preferExternal"]>
  8. ...
  9. </manifest>


官方API:

android:installLocation
The default install location for the application.

The following keyword strings are accepted:

Value Description
"internalOnly" The application must be installed on the internal device storage only. If this is set, the application will never be installed on the external storage. If the internal storage is full, then the system will not install the application. This is also the default behavior if you do not defineandroid:installLocation.
"auto" The application may be installed on the external storage, but the system will install the application on the internal storage by default. If the internal storage is full, then the system will install it on the external storage. Once installed, the user can move the application to either internal or external storage through the system settings.
"preferExternal" The application prefers to be installed on the external storage (SD card). There is no guarantee that the system will honor this request. The application might be installed on internal storage if the external media is unavailable or full, or if the application uses the forward-locking mechanism (not supported on external storage). Once installed, the user can move the application to either internal or external storage through the system settings.


android:installLocation可以设置为"auto"、"internalOnly"、"preferExternal"三个值中的任何一个.

  auto:程序可能被安装在外部存储介质上(例如:SD Card),但是默认会被安装到手机内存中.当手机内存为空时,程序将被安装到外部存储介质上.当程序安装到手机上后,用户可以决定把程序放在外部储介质还是内存中.

  internalOnly:默认值.当设置为该值时,程序只能被安装在内存中,如果内存为空,则程序将不能成功安装.

  preferExternal:将程序安装在外部存储介质上,但是系统不保证程序一定会被安装到外部存储介质上.当外部存储介质不可以或空时,程序将被安装到内存中.程序使用了forward-locking机制时也将被安装到内存中,因为外部存储不支持此机制.程序安装后,用户可以自由切换程序应该在外部还是内部存储介质上.


注意:当程序使用了Google Play的Copy Protection特性时,只能安装到内存中.

当程序被安装到外部存储介质时,

.apk文件将被移动到外部存储介质上,但是程序的数据仍然会在内存中

保存.apk文件的容器将会使用一个随机生成的密钥进行加密,这样只有安装该程序的设置可以使用存在外部存储介质上的数据.


警告:当外部存储介质被卸载时,安装在该外部存储介质上的程序将立刻被终止掉!

向后兼容性:

  声明了android:installLocation,但android:minSdkVersion小于8时,我们使用不低于Froyo的AVD进行编译,这样在低于Froyo的系统中android:installLocation将被忽略,而不低于Froyo的系统中将使用我们指定的android:installLocation.


建议:
当程序具有如下行为时我们不应该将程序安装到外部存储介质上

  ①Service

    正在运行的服务将被终止,当外部存储介质被重新加载时服务不会被重启.

  ②Alarm Service

    闹钟服务将被取消,开发者必须在外部存储介质重新加载后重新注册闹钟服务.

  ③Input Method Engines

    输入法将被换成系统输入法,当外部存储介质被重新加载后用户可以通过系统设置来启动我们的输入法

  ④Live Wallpapers

    我们的动态壁纸将被替换为默认的动态壁纸.外部存储介质重载后,用户可以更换回来.

  ⑤Live Folders

    我们的动态文件夹将被移出.

  ⑥App Widgets

    我们的小部件将被移出,通常只有系统重启后我们的小部件才可用.

  ⑦Account Managers

    使用AccountManager创建的账户将会消失,直至存储介质被重新加载.

  ⑧Sync Adapters

    只有外部存储介质被重新加载时,我们的同步功能才可用

  ⑨Device Administrators

    我们的DeviceAdminReceiver将会失效

  ⑩监听开机结束事件


    系统会在加载外部存储介质之前发送ACTION_BOOT_COMPLETED广播.因此安装在外部存储介质的程序将不能接受开机广播.

通常,只要我们没有使用上述的特性,我们就可以将程序安装到外部存储介质上.例如,大的游戏程序.当APK文件很大时我们应该认真的考虑是否要将程序移动到外部存储介质上以帮助用户节省内存.


更多相关文章

  1. Android控制台中运行Java程序
  2. 程序员也是会浪漫的->打造浪漫的Android表白程序
  3. 【Android 内存优化】Bitmap 内存缓存 ( Bitmap 内存复用 | 弱引
  4. 关于android 4.4以上版本从相册选取图片加载不了图片的问题
  5. Android中 加载一张大图片Caused by: java.lang.OutOfMemoryErro
  6. Android 异步获取网络图片并处理图片Out Of Memory 内存溢出问题
  7. ANDROID – 使用特定的URL开启应用程序
  8. Android点击2次返回按钮退出应用程序

随机推荐

  1. Android下Xml解析技术(四)、pull生成Xml文
  2. get方式和post方式的请求
  3. Android(安卓)ActionBar与ViewPager合用
  4. 较深入的分析Content Providers用法
  5. Android(安卓)ListView元素间隙线自定义
  6. Android传感器的介绍
  7. Android(安卓)实时视频采集—Cameara预览
  8. Android消息推送(广播机制)+通知
  9. android手记之--广播接收者
  10. 【android积累】