Android应用开发中实现apk皮肤文件换肤的思路分析

所属分类: 软件编程 / Android 阅读数: 1336
收藏 0 赞 0 分享

在android的项目开发中,都会遇到后期功能拓展增强与主程序代码变更的现实矛盾,也就是程序的灵活度。 由于linux平台的安全机制,再加上dalvik的特殊机制,各种权限壁垒,使得开发一个灵活多变的程序,变得比较困难,不像pc平台下那么容易。
这里实际上可以借鉴传统软件中扩展程序的方法: 也就是插件的实现. 如目前所有的浏览器,比如我们使用的eclipse,以及很多优秀的软件,都使用了此种方式. 这样轻松实现了软件的功能扩展,而升级功能时只用更新对应插件, 而不是需要更新整个应用,降低了程序的耦合度.
而在Android中的实现思路,即为将一个较大的APK,分离为一个主程序的APK,和其他各种较小的APK.
      
典型的应用为手机QQ换肤的实现方式:
QQ的皮肤是一个无界面APK应用,这个皮肤应用中的资源和主程序的资源命名一致,通过主程序和皮肤程序共享进程实现主程序对皮肤程序中资源的访问,在程序运行时通过代码显示指定皮肤资源,缺点是在主程序中每个activity要增加复杂的使用哪种皮肤逻辑
      
本例实现效果如下:

2016226154309441.jpg (371×533)

2016226154731906.jpg (370×528)

下面分析下具体思路:
      android下,默认的情况是,每个apk相互独立的,基本上每个应用都是一个dalvik虚拟机,都有一个uid,再配合上linux本身的权限机制,使得apk互通很难直接进行。但作为一个独立应用的集成,不管多少个apk,都可以并为一个单独的dalvik虚拟机,直观的反映给开发人员就是在shell下列出进程,那几个apk同时加载后,会一个进程存在。
        可以在清单文件中加入如下配置:

   android:sharedUserId="com.tony.test"

      
         android:sharedUserId是指共用一个uid,也就是,凡是这个属性相同的工程,都会共用同一个uid,这样,权限壁垒就消除了,dalvik也会融合为一个,可以测试一下,写几个工程,没有这个属性和有这个属性的情况下,同时运行,在列出当前进程,就直观的说明了。
        
下面还是用代码说明,一共分为两部分. 主程序 Re_Skin和皮肤程序Re_Skin1
首先是主应用程序代码:
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;(主要的皮肤更换逻辑实现类)

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>)); 
          } 
        }); 
      } 
    }); 
  } 
} 

皮肤应用中不需要界面显示
这个皮肤应用中的资源和主程序的资源命名一致即可.
清单文件:

<?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> 

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多