Android to build your own personalization app (iii): Application plug-in

Source: Internet
Author: User
Tags reskin

In the development of Android project, we will encounter the real contradiction between the late function expansion and the main program code change, which is the flexibility of the program. Due to the security mechanism of Linux platform, coupled with the special mechanism of Dalvik, various authority barriers, it makes it more difficult to develop a flexible and changeable program, which is not as easy as PC platform.

This can actually be used to learn from traditional software extension programs: that is, plug-in implementation. This is used for all current browsers, such as Eclipse, which we use, and a lot of good software. This makes it easy to extend the functionality of the software, while upgrading the function only with the update of the corresponding plug-in, rather than need to update the entire application, reduce the program's coupling degree.

And the idea of implementation in Android, that is, to put a larger apk, separated into a main program apk, and other various smaller apk.

Typical application for mobile phone QQ skin Change implementation way:

QQ Skin is a no interface apk application, this skin application resources and the main program resource naming consistent, through the main program and the skin program sharing process to achieve the main program of the skin Program access to resources, in the program run through the code to display the specified skin resources, The disadvantage is that each activity in the main program adds complexity to the use of which skin logic

This example achieves the following effect:

The following analysis of specific ideas:

Under Android, the default is that each apk is independent of each other, basically each application is a Dalvik virtual machine, there is a UID, and then with the Linux itself permissions mechanism, so that apk interoperability is difficult to directly. But as a standalone application of integration, no matter how many apk, can and for a separate Dalvik virtual machine, the visual reflection to the developer is the shell under the process, those several apk loaded at the same time, a process exists.

The following configuration can be added to the manifest file:

     Android:shareduserid= "Com.tony.test"

Android:shareduserid refers to a common UID, that is, all the same project, will share the same UID, so that the authority barrier is eliminated, Dalvik will be fused to a, you can test, write a few works, Without this attribute and having this attribute in the case, running concurrently, in listing the current process, is intuitively illustrated.

The following is also a code description, divided into two parts. Main program Re_skin and skin program Re_skin1

The first is the main application code:

1. manifest file Androidmanifest.xml:

[Java]View Plaincopy
  1. <?xml version="1.0" encoding="Utf-8"?>
  2. <manifest xmlns:android="Http://schemas.android.com/apk/res/android"
  3. package="Com.tony.reskin"
  4. android:versioncode="1"
  5. Android:versionname="1.0" <span style="color: #FF0000;" >android:shareduserid="Com.tony.skin" </span>>
  6. <USES-SDK android:minsdkversion="7"/>
  7. <application android:icon="@drawable/icon" android:label="@string/app_name" >
  8. <activity android:name="Com.tony.reskin.Re_SkinActivity"
  9. Android:label="@string/app_name" >
  10. <intent-filter>
  11. <action android:name="Android.intent.action.MAIN"/>
  12. <category android:name="Android.intent.category.LAUNCHER"/>
  13. </intent-filter>
  14. </activity>
  15. </application>
  16. </manifest>


2. layout file:

[Java]View Plaincopy
  1. <?xml version="1.0" encoding="Utf-8"?>
  2. <linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"
  3. android:orientation="Vertical"
  4. Android:layout_width="Fill_parent"
  5. android:layout_height="Fill_parent"
  6. android:id="@+id/layout" >
  7. <textview
  8. Android:layout_width="Fill_parent"
  9. android:layout_height="Wrap_content"
  10. android:text="@string/hello"
  11. />
  12. <button android:text="Set" android:id="@+id/button1" android:layout_width= "wrap_content" Android: layout_height="wrap_content" ></Button>
  13. </LinearLayout>


3. re_skinactivity; (Main skin change logic implementation Class)

[Java]View Plaincopy
  1. Package Com.tony.reskin;
  2. Import android.app.Activity;
  3. Import Android.content.Context;
  4. Import android.content.pm.PackageManager.NameNotFoundException;
  5. Import Android.os.Bundle;
  6. Import Android.os.Handler;
  7. Import Android.view.View;
  8. Import Android.view.View.OnClickListener;
  9. Import Android.widget.Button;
  10. Import Android.widget.LinearLayout;
  11. Public class Re_skinactivity extends Activity {
  12. private LinearLayout layout;
  13. private Button Btnset;
  14. <span style="color: #FF0000;" >private Context friendcontext;</span>
  15. /** Called when the activity is first created. * /
  16. @Override
  17. public void OnCreate (Bundle savedinstancestate) {
  18. super.oncreate (savedinstancestate);
  19. Setcontentview (R.layout.main);
  20. Btnset = (Button) Findviewbyid (R.id.button1);
  21. Layout = (linearlayout) Findviewbyid (r.id.layout);
  22. Layout.setbackgroundresource (r.drawable.bg);
  23. try {
  24. <span style="color: #FF0000;"  >friendcontext = Createpackagecontext ("Com.tony.reskin1", context.context_ignore_security);</span>
  25. } catch (Namenotfoundexception e) {
  26. E.printstacktrace ();
  27. }
  28. Btnset.setonclicklistener (new Onclicklistener () {
  29. @Override
  30. public void OnClick (View v) {
  31. new Handler (). Post (new Runnable () {
  32. @Override
  33. public Void Run () {
  34. Layout.setbackgrounddrawable (<span style="color: #FF0000;"  >friendcontext.getresources (). getdrawable (r.drawable.bg</span>));
  35. }
  36. });
  37. }
  38. });
  39. }
  40. }


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:

[Java]View Plaincopy
    1. <?xml version="1.0" encoding="Utf-8"?>
    2. <manifest xmlns:android="Http://schemas.android.com/apk/res/android"
    3. package="com.tony.reskin1"
    4. android:versioncode="1"
    5. Android:versionname="1.0" <span style="color: #FF0000;" >android:shareduserid="Com.tony.skin" </span>>
    6. <USES-SDK android:minsdkversion="7"/>
    7. <application android:icon="@drawable/icon" android:label="@string/app_name" >
    8. <activity android:name=". Re_skin1activity "
    9. Android:label="@string/app_name" >
    10. <intent-filter>
    11. <action android:name="Android.intent.action.MAIN"/>
    12. <category android:name="Android.intent.category.LAUNCHER"/>
    13. </intent-filter>
    14. </activity>
    15. </application>
    16. </manifest>

Android to build your own personalization app (iii): Application plug-in

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.