在android的項(xiàng)目開發(fā)中,都會(huì)遇到后期功能拓展增強(qiáng)與主程序代碼變更的現(xiàn)實(shí)矛盾,也就是程序的靈活度。 由于linux平臺(tái)的安全機(jī)制,再加上dalvik的特殊機(jī)制,各種權(quán)限壁壘,使得開發(fā)一個(gè)靈活多變的程序,變得比較困難,不像pc平臺(tái)下那么容易。
這里實(shí)際上可以借鑒傳統(tǒng)軟件中擴(kuò)展程序的方法: 也就是插件的實(shí)現(xiàn). 如目前所有的瀏覽器,比如我們使用的eclipse,以及很多優(yōu)秀的軟件,都使用了此種方式. 這樣輕松實(shí)現(xiàn)了軟件的功能擴(kuò)展,而升級(jí)功能時(shí)只用更新對(duì)應(yīng)插件, 而不是需要更新整個(gè)應(yīng)用,降低了程序的耦合度.
而在Android中的實(shí)現(xiàn)思路,即為將一個(gè)較大的APK,分離為一個(gè)主程序的APK,和其他各種較小的APK.
典型的應(yīng)用為手機(jī)QQ換膚的實(shí)現(xiàn)方式:
QQ的皮膚是一個(gè)無界面APK應(yīng)用,這個(gè)皮膚應(yīng)用中的資源和主程序的資源命名一致,通過主程序和皮膚程序共享進(jìn)程實(shí)現(xiàn)主程序?qū)ζつw程序中資源的訪問,在程序運(yùn)行時(shí)通過代碼顯示指定皮膚資源,缺點(diǎn)是在主程序中每個(gè)activity要增加復(fù)雜的使用哪種皮膚邏輯
本例實(shí)現(xiàn)效果如下:
下面分析下具體思路:
android下,默認(rèn)的情況是,每個(gè)apk相互獨(dú)立的,基本上每個(gè)應(yīng)用都是一個(gè)dalvik虛擬機(jī),都有一個(gè)uid,再配合上linux本身的權(quán)限機(jī)制,使得apk互通很難直接進(jìn)行。但作為一個(gè)獨(dú)立應(yīng)用的集成,不管多少個(gè)apk,都可以并為一個(gè)單獨(dú)的dalvik虛擬機(jī),直觀的反映給開發(fā)人員就是在shell下列出進(jìn)程,那幾個(gè)apk同時(shí)加載后,會(huì)一個(gè)進(jìn)程存在。
可以在清單文件中加入如下配置:
android:sharedUserId="com.tony.test"
android:sharedUserId是指共用一個(gè)uid,也就是,凡是這個(gè)屬性相同的工程,都會(huì)共用同一個(gè)uid,這樣,權(quán)限壁壘就消除了,dalvik也會(huì)融合為一個(gè),可以測試一下,寫幾個(gè)工程,沒有這個(gè)屬性和有這個(gè)屬性的情況下,同時(shí)運(yùn)行,在列出當(dāng)前進(jìn)程,就直觀的說明了。
下面還是用代碼說明,一共分為兩部分. 主程序 Re_Skin和皮膚程序Re_Skin1
首先是主應(yīng)用程序代碼:
1. 清單文件AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tony.reskin" android:versionCode="1" android:versionName="1.0" <span style="color:#FF0000;">android:sharedUserId="com.tony.skin"</span>> <uses-sdk android:minSdkVersion="7" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name="com.tony.reskin.Re_SkinActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
2. 布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/layout" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:text="Set" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout>
3. Re_SkinActivity;(主要的皮膚更換邏輯實(shí)現(xiàn)類)
package com.tony.reskin; import android.app.Activity; import android.content.Context; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; public class Re_SkinActivity extends Activity { private LinearLayout layout; private Button btnSet; <span style="color:#FF0000;">private Context friendContext;</span> /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnSet = (Button)findViewById(R.id.button1); layout = (LinearLayout)findViewById(R.id.layout); layout.setBackgroundResource(R.drawable.bg); try { <span style="color:#FF0000;">friendContext = createPackageContext("com.tony.reskin1", Context.CONTEXT_IGNORE_SECURITY);</span> } catch (NameNotFoundException e) { e.printStackTrace(); } btnSet.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new Handler().post(new Runnable() { @Override public void run() { layout.setBackgroundDrawable(<span style="color:#FF0000;">friendContext.getResources().getDrawable(R.drawable.bg</span>)); } }); } }); } }
皮膚應(yīng)用中不需要界面顯示
這個(gè)皮膚應(yīng)用中的資源和主程序的資源命名一致即可.
清單文件:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tony.reskin1" android:versionCode="1" android:versionName="1.0" <span style="color:#FF0000;">android:sharedUserId="com.tony.skin"</span>> <uses-sdk android:minSdkVersion="7" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Re_Skin1Activity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
新聞熱點(diǎn)
疑難解答
圖片精選