Android App Theme (ii)

Source: Internet
Author: User

The second way to implement Android app theme is through APK. Here is a demo.

1. First you must create a new apk, similar plugin, and then add a meta-data to the application of the APK androidmanifest.xml file. This is the next step to find out if it is your own plugin apk to make a mark.

<meta-data android:name= "Skin_demo2_plugin" android:value= "Com.example.skindemo2.icon"/>
2. In the apk add some pictures, also can use other (including style, etc.) here is mainly for demonstration. Define a method in the mainactivity of the APK. The main purpose here is to facilitate the main apk to quickly call this method.

static int alldrawableid[] = {R.drawable.img_apparel_accessories,r.drawable.img_computers_software,r.drawable.img_ Electro,r.drawable.img_electronics,r.drawable.img_entertaiment,r.drawable.img_food_beverage,r.drawable.img_ Kids_baby,r.drawable.img_sport,r.drawable.img_toys_games};p ublic int getdrawableidforotherapp (int position) {if ( Position < Alldrawableid.length) {return alldrawableid[position];} return 0;}

3. In the main apk, by reading all the installed apps, analyze ApplicationInfo's meta-data and then decide if it's your own plugin apk.

The context of the corresponding plugin is then obtained via Context.createpackagecontext (Packagename,int flag). It then gets the class class of mainactivity through the ClassLoader, and then gets the value returned by the method through reflection. You can then get the Drawable object.

Package Com.example.skindemo2;import Java.lang.reflect.method;import Java.util.arraylist;import java.util.List; Import Android.os.asynctask;import android.os.bundle;import android.app.activity;import android.content.Context; Import Android.content.pm.applicationinfo;import Android.content.pm.packageinfo;import Android.content.pm.packagemanager;import Android.content.pm.packagemanager.namenotfoundexception;import Android.graphics.drawable.drawable;import Android.view.view;import Android.widget.button;import Android.widget.imageview;public class Mainactivity extends Activity {private Button mswtichimgbgbtn;private ImageView Mshowimg;private Context context;private arraylist<string> mpluginpackagenamelist = new ArrayList<String> ();p rivate int position = 0; @Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); context = This;mswtichimgbgbtn = (Button) This.findviewbyid (R.ID.SWTICHIMGBGBTN); MSWTICHIMGBGbtn.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View v) {swtichimguseplugin ();}}); Mshowimg = (ImageView) This.findviewbyid (r.id.myshowimg); new asynctask<void, Void, void> () {@Overrideprotected void Doinbackground (void ... params) {findpluginpackagename (); return null;} @Overrideprotected void OnPostExecute (void result) {swtichimguseplugin (); Super.onpostexecute (result);}}. Execute ();} private void Findpluginpackagename () {Packagemanager pm = Context.getpackagemanager (); list<packageinfo> list = pm.getinstalledpackages (0);//Get all installed APK's packageinfostring meta = null; ApplicationInfo ainfo = null;for (PackageInfo pi:list) {String pkgname = pi.packagename;try {ainfo = Pm.getApplicationIn Fo (pkgname,packagemanager.get_meta_data);} catch (Namenotfoundexception e) {e.printstacktrace ();}  if (Ainfo = = NULL | | ainfo.metadata = = NULL) Continue;meta = aInfo.metaData.getString ("Skin_demo2_plugin"); if (meta = = NULL || ". Equals (meta)) continue;if (" Com.example.skindEmo2.icon ". Equals (meta)) {//Whether it is your own plugin Apkmpluginpackagenamelist.add (ainfo.packagename) by the value of meta);}} You must operate private void Swtichimguseplugin () in the main thread () {//I am here to take the first plugin APK data in order to facilitate it, in fact, the user chooses the APK to switch the IF ( Mpluginpackagenamelist.size ()! = 0) {try {//through Createpackagecontext method get plugin apk contextcontext otherpluscontext = Context.createpackagecontext (mpluginpackagenamelist.get (0), context.context_include_code| context.context_ignore_security);//By getting the plugin APK's CONTEXT class loader, and then getting Mainactvity class object class<?> MainClass = Otherpluscontext.getclassloader (). LoadClass (Mpluginpackagenamelist.get (0) + ". Mainactivity "),//Drawablemethod m = Mainclass.getmethod (" Getdrawableidforotherapp ", Int.class) by reflection to obtain the corresponding position; int Imgbgid = (Integer) m.invoke (Mainclass.newinstance (), position),//In order to be able to cycle through the img if (imgbgid = 0) {position = 0;} else{position + = 1;}//Get drawable object drawable drawable = Otherpluscontext.getresources (). getdrawable (Imgbgid); Mshowimg.setimagedrawable (drawable);} catch (Exception e) {e.printstacktrace ();}}}}





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.