Android 制定安裝重寫遷移至SD卡 APP2SD

來源:互聯網
上載者:User

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

大家都知道寫部落格會很累的,希望

轉載請註明出處: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卡存在,則有系統決定要安裝的位置。

運行結果:

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.