Android應(yīng)用開發(fā)中實現(xiàn)apk皮膚文件換膚的思路分析
在android的項目開發(fā)中,都會遇到后期功能拓展增強與主程序代碼變更的現(xiàn)實矛盾,也就是程序的靈活度。 由于linux平臺的安全機制,再加上dalvik的特殊機制,各種權(quán)限壁壘,使得開發(fā)一個靈活多變的程序,變得比較困難,不像pc平臺下那么容易。
這里實際上可以借鑒傳統(tǒng)軟件中擴展程序的方法: 也就是插件的實現(xiàn). 如目前所有的瀏覽器,比如我們使用的eclipse,以及很多優(yōu)秀的軟件,都使用了此種方式. 這樣輕松實現(xiàn)了軟件的功能擴展,而升級功能時只用更新對應(yīng)插件, 而不是需要更新整個應(yīng)用,降低了程序的耦合度.
而在Android中的實現(xiàn)思路,即為將一個較大的APK,分離為一個主程序的APK,和其他各種較小的APK.
典型的應(yīng)用為手機QQ換膚的實現(xiàn)方式:
QQ的皮膚是一個無界面APK應(yīng)用,這個皮膚應(yīng)用中的資源和主程序的資源命名一致,通過主程序和皮膚程序共享進程實現(xiàn)主程序?qū)ζつw程序中資源的訪問,在程序運行時通過代碼顯示指定皮膚資源,缺點是在主程序中每個activity要增加復(fù)雜的使用哪種皮膚邏輯
本例實現(xiàn)效果如下:


下面分析下具體思路:
android下,默認的情況是,每個apk相互獨立的,基本上每個應(yīng)用都是一個dalvik虛擬機,都有一個uid,再配合上linux本身的權(quán)限機制,使得apk互通很難直接進行。但作為一個獨立應(yīng)用的集成,不管多少個apk,都可以并為一個單獨的dalvik虛擬機,直觀的反映給開發(fā)人員就是在shell下列出進程,那幾個apk同時加載后,會一個進程存在。
可以在清單文件中加入如下配置:
android:sharedUserId="com.tony.test"
android:sharedUserId是指共用一個uid,也就是,凡是這個屬性相同的工程,都會共用同一個uid,這樣,權(quán)限壁壘就消除了,dalvik也會融合為一個,可以測試一下,寫幾個工程,沒有這個屬性和有這個屬性的情況下,同時運行,在列出當前進程,就直觀的說明了。
下面還是用代碼說明,一共分為兩部分. 主程序 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;(主要的皮膚更換邏輯實現(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)用中不需要界面顯示
這個皮膚應(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>
相關(guān)文章
Android CountDownTimer實現(xiàn)定時器和倒計時效果
這篇文章主要為大家詳細介紹了Android CountDownTimer實現(xiàn)定時器和倒計時效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-02-02
Okhttp3實現(xiàn)爬取驗證碼及獲取Cookie的示例
本篇文章主要介紹了Okhttp3實現(xiàn)爬取驗證碼及獲取Cookie的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
Flutter實現(xiàn)webview與原生組件組合滑動的示例代碼
這篇文章主要介紹了Flutter實現(xiàn)webview與原生組件組合滑動的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03

