Android builds its own personalized applications (3): plug-ins of applications

Source: Internet
Author: User
Tags reskin

In Android project development, there will be real conflicts between the later feature expansion enhancement and the main program code change, that is, the flexibility of the program. The Linux platform's security mechanism, coupled with the special Dalvik mechanism, and various permission barriers make it difficult to develop a flexible and variable program, unlike the PC platform.

Here we can actually use the extension method in traditional software: plug-in implementation. for example, all browsers, such as eclipse, and many excellent software, use this method. in this way, the function expansion of the software is easily realized, and only the corresponding plug-ins are updated when the function is upgraded, instead of the entire application, which reduces the Coupling Degree of the program.

The idea of implementation in Android is to separate a large APK into an APK of the main program and a variety of other smaller APK.

The typical application is the mobile QQ skin replacement method:

QQ's skin is a non-interface APK application. The resources in this skin application are named the same as those in the main program, through the main program and skin program sharing process, the main program can access the resources in the skin program. When the program is running, the Code displays the specified skin resources, the disadvantage is that in the main program, each activity needs to add the complicated skin logic to be used.

In this example, the effect is as follows:


The following describes the specific ideas:

In Android, by default, each APK is independent of each other. Basically, each application is a Dalvik virtual machine with a uid, Which is used together with the Linux permission mechanism, this makes it difficult to directly connect the APK. However, as an Independent Application Integration, no matter how many APK files can be combined into a single Dalvik virtual machine, which is intuitively reflected to developers as listing processes under shell, after these APK files are loaded at the same time, a process will exist.

You can add the following configuration to the configuration file:

     android:sharedUserId="com.tony.test"

Android: shareduserid refers to sharing a uid, that is, all projects with the same property share the same uid. In this way, permission barriers are eliminated and Dalvik is integrated into one, you can test it and write several projects. If this property is not available and the property is run at the same time, the current process will be listed.

The following code shows two parts: the main program re_skin and the skin program re_skin1.

First, the main application code:

1. list file 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" 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. 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 implementation class of skin replacement logic)

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() {@Overridepublic void onClick(View v) {new Handler().post(new Runnable() {@Overridepublic void run() {layout.setBackgroundDrawable(friendContext.getResources().getDrawable(R.drawable.bg));}});}});    }}

Interface display is not required for skin applications

The resources in this skin application are named the same as those in the main program.

Configuration 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" 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>

 

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.