/**********************************************************
大家都知道写博客会很累的,希望
转载请注明出处:http://blog.csdn.net/ta893115871
请不要可怜你的鼠标,(*^__^*) 嘻嘻……
*************************************************************/
在SDK2.2发布之前,Android安装应用程序仅能安装在手机内存中,而且在有限的资源下,至多能安装50~100套程序,若是把“Angry Brid” 愤怒的小鸟安装到手机上。那么可以
而知了。
当安装程序时发现有SD卡时,会自动安装于SD卡上,如没有则通过按钮事件,前去应用程序设置,有User决定是否要将应用程序移置Sd卡或是将程序从SD卡移回手机。
.java文件
package com.example.app2sd; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class App2SDActivity extends Activity { private Button mButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mButton=(Button)this.findViewById(R.id.button_id); mButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub //通过按钮事件,前去应用程序设置,有User决定是否要将应用程序移置Sd卡 startActivity(new Intent("android.intent.action.MANAGE_PACKAGE_STORAGE")); } }); } }
AndroidManifest.xml
本程序程序的关键在于AndroidMainfest.xml里的 android:installLocation="preferExternal"这个属性。设置为preferExternal,让程序默认安装在SD卡上;另一个重点是在于本程序的API Level需要制定android:minSdkVersion为“8”以上的版本才支持。
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.app2sd" android:versionCode="1" android:versionName="1.0" android:installLocation="preferExternal" > <!-- move app to sd card you should in mainfest to add android:installLocation="preferExternal" and sdk should >=8 that version is 2.2 . this function added in after 2.2 version. --> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".App2SDActivity" android:label="@string/title_activity_app2_sd" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
如果你用的旧的SDK版本开发且制定android:minSdkVersion小于8的话,直接在AndroidMainfest.xml里加上 android:installLocation="preferExternal"属性,结果会怎么?
系统无法识别 android:installLocation="preferExternal"这个属性。必须API Level 至少为8以上才行。
此外, android:installLocation实行2个值可以设置,在<mainfest>里定义的方法如下:
android:installLocation="auto"
android:installLocation="preferExternal"
如果你更改为“auto”,那么才行会默认安装在手机内置内存中,但程序也会依据手机的最佳位置,如发现手机的内存偏低且又有Sd卡存在,则有系统决定要安装的位置。
运行结果: