Android 打造自己的個人化應用(三):應用程式的外掛程式化

來源:互聯網
上載者:User

在android的項目開發中,都會遇到後期功能拓展增強與主程式碼變更的現實矛盾,也就是程式的靈活度。 由於linux平台的安全機制,再加上dalvik的特殊機制,各種許可權壁壘,使得開發一個靈活多變的程式,變得比較困難,不像pc平台下那麼容易。
        這裡實際上可以借鑒傳統軟體中擴充程式的方法: 也就是外掛程式的實現. 如目前所有的瀏覽器,比如我們使用的eclipse,以及很多優秀的軟體,都使用了此種方式. 這樣輕鬆實現了軟體的功能擴充,而升級功能時只用更新對應外掛程式, 而不是需要更新整個應用,降低了程式的耦合度.


        而在Android中的實現思路,即為將一個較大的APK,分離為一個主程式的APK,和其他各種較小的APK.


      


        典型的應用為手機QQ換膚的實現方式:


       QQ的皮膚是一個無介面APK應用,這個皮膚應用中的資源和主程式的資源命名一致,通過主程式和皮膚程式共用進程實現主程式對皮膚程式中資源的訪問,在程式運行時通過代碼顯示指定皮膚資源,缺點是在主程式中每個activity要增加複雜的使用哪種皮膚邏輯

      


      本例實現效果如下:

    

 

 

 

 


      

 

 

       下面分析下具體思路:

      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:


[java]
<?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> 

<?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" android:sharedUserId="com.tony.skin">
    <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. 布局檔案:


[java]
<?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> 

<?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;(主要的皮膚更換邏輯實作類別)


[java]
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>)); 
                    } 
                }); 
            } 
        }); 
    } 
} 

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;
 private Context friendContext;
    /** 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 {
   friendContext =  createPackageContext("com.tony.reskin1", Context.CONTEXT_IGNORE_SECURITY);
  } 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(friendContext.getResources().getDrawable(R.drawable.bg));
     }
    });
   }
  });
    }
}
皮膚應用中不需要介面顯示

這個皮膚應用中的資源和主程式的資源命名一致即可.

資訊清單檔:


[java]
<?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> 

<?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" android:sharedUserId="com.tony.skin">
    <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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.