Android App Theme (ii)

Source: Internet
Author: User

Another way to implement Android app theme is through APK.

Here is a demo.

1. You must first create a new apk. Like the plugin, then add a meta-data to the application of the APK's 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 of this method is to facilitate the main apk high speed call.

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 installed apps, analyze ApplicationInfo's meta-data. Then infer whether it is your own plugin apk.

The context of the corresponding plugin is then obtained via Context.createpackagecontext (Packagename,int flag). Then we get the class class of mainactivity through the loader. The values returned by the method are then reflected. 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)) {//The value of meta is inferred whether it is its own plug-in Apkmpluginpackagenamelist.add (Ainfo.packagename);}}} You must operate private void Swtichimguseplugin () in the main thread () {//I have taken the first plug-in APK data directly for convenience. In practice, the user chooses the APK to toggle the IF (mpluginpackagenamelist.size ()! = 0) {try {//Createpackagecontext method to get the plugin apk contextcontext Otherpluscontext = Context.createpackagecontext (mpluginpackagenamelist.get (0), context.context_include_code| context.context_ignore_security);//By getting 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) of the corresponding position by reflection; 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 ();}}}}


watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvahvhbmd5yw5iaw4xmjm=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/center ">



Android App Theme (ii)

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.