Android應(yīng)用開(kāi)發(fā)中實(shí)現(xiàn)apk皮膚文件換膚的思路分析
在android的項(xiàng)目開(kāi)發(fā)中,都會(huì)遇到后期功能拓展增強(qiáng)與主程序代碼變更的現(xiàn)實(shí)矛盾,也就是程序的靈活度。 由于linux平臺(tái)的安全機(jī)制,再加上dalvik的特殊機(jī)制,各種權(quán)限壁壘,使得開(kāi)發(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è)無(wú)界面APK應(yīng)用,這個(gè)皮膚應(yīng)用中的資源和主程序的資源命名一致,通過(guò)主程序和皮膚程序共享進(jìn)程實(shí)現(xiàn)主程序?qū)ζつw程序中資源的訪問(wèn),在程序運(yùn)行時(shí)通過(guò)代碼顯示指定皮膚資源,缺點(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ī),直觀的反映給開(kāi)發(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è),可以測(cè)試一下,寫幾個(gè)工程,沒(méi)有這個(gè)屬性和有這個(gè)屬性的情況下,同時(shí)運(yùn)行,在列出當(dāng)前進(jìn)程,就直觀的說(shuō)明了。
下面還是用代碼說(shuō)明,一共分為兩部分. 主程序 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>
相關(guān)文章
用xutils3.0進(jìn)行下載項(xiàng)目更新
這篇文章主要介紹了用xutils3.0進(jìn)行下載項(xiàng)目更新的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08
Android CountDownTimer實(shí)現(xiàn)定時(shí)器和倒計(jì)時(shí)效果
這篇文章主要為大家詳細(xì)介紹了Android CountDownTimer實(shí)現(xiàn)定時(shí)器和倒計(jì)時(shí)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
Okhttp3實(shí)現(xiàn)爬取驗(yàn)證碼及獲取Cookie的示例
本篇文章主要介紹了Okhttp3實(shí)現(xiàn)爬取驗(yàn)證碼及獲取Cookie的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
Spinner在Dialog中的使用效果實(shí)例代碼詳解
這篇文章主要介紹了Spinner在Dialog中的使用效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05
Flutter實(shí)現(xiàn)webview與原生組件組合滑動(dòng)的示例代碼
這篇文章主要介紹了Flutter實(shí)現(xiàn)webview與原生組件組合滑動(dòng)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Android實(shí)現(xiàn)點(diǎn)擊縮略圖放大效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)點(diǎn)擊縮略圖放大效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09

