An analysis of the idea of apk skin file in the development of Android application _android

Source: Internet
Author: User
Tags reskin

In the Android project development, there will be a real contradiction between the later function expansion and the main program code changes, that is, the flexibility of the program. Because of Linux platform security mechanism, plus Dalvik special mechanism, various authority barriers, make the development of a flexible program, become more difficult, unlike PC platform so easy.
Here you can actually learn from the traditional software Extender method: that is, the implementation of Plug-ins. All browsers, such as the eclipse we use, and a lot of good software, use this approach. This is easy to achieve the software expansion, and upgrade function only with the update of the corresponding plug-ins, rather than need to update the entire application, reducing the coupling degree of the program.
The idea in Android is to split a larger apk into a main program of APK, and other smaller apk.

Typical application for mobile phone QQ skin-changing way to achieve:
QQ Skin is a no interface apk application, this skin application resources and the main program resource naming consistent, through the main program and skin program sharing process to achieve the main program in the skin Program access to resources, in the program running through the code to display the specified skin resources, The disadvantage is that each activity in the main program increases the complexity of what kind of skin logic to use

This example achieves the following effect:

The following analysis of specific ideas:
Android, the default is that each apk mutually independent, basically each application is a Dalvik virtual machine, have a UID, and then with Linux itself, the authority of the mechanism, making APK interoperability is difficult to directly. But as an independent application of integration, no matter how many apk, can and for a separate Dalvik virtual machine, intuitive reflection to the developer is in the shell below the process, the several apk loaded at the same time, a process exists.
You can include the following configuration in the manifest file:

   Android:shareduserid= "Com.tony.test"


Android:shareduserid refers to the common use of a UID, that is, all the same properties of the same project, will share the same UID, so that the authority barrier is eliminated, Dalvik will also be merged into a, you can test, write a few works, Without this attribute and with this attribute, running at the same time, listing the current process, is intuitively illustrated.

The following is a code description, divided into two parts altogether. Main program Re_skin and skin program Re_skin1
The first is the main application code:
1. manifest Document 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. layout file:

<?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 (main skin replacement logic implementation Class)

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;" The >private context friendcontext;</span>/** called the "when" is the "the". 
    * * @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.printStackTra 
    CE (); Btnset.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {NE W Handler (). Post (new Runnable () {@Override public void run () {Layout.setbackgrounddra Wable (<span style= "color: #FF0000;" 
          >friendcontext.getresources (). getdrawable (r.drawable.bg</span>)); 
      } 
        }); 
  } 
    }); 

 } 
}

No interface display required in skin applications
The resources in this skin application are named consistent with the resources of the main program.
Manifest file:

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.