Android Theme App Theme implementation

Source: Internet
Author: User

Android implementationThe way to apply 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};

public int Getdrawableidforotherapp (int position) {

if (position < alldrawableid.length) {
return alldrawableid[position];
}
return 0;
}

3. In the main apk by reading all the installed apps by analyzing the ApplicationInfo meta-data and then judging if it's your plugin apk.
 
then gets the context of the corresponding plugin 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> ();
 private int position = 0;

  @Override
 protected 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 () {
   
   @ Override
   public void OnClick (View v) {
    swtichimguseplugin ();
   }
 });
  mshowimg = (ImageView) This.findviewbyid (r.id.myshowimg);
  New asynctask<void, Void, void> () {

@Override
protected void Doinbackground (void ... params) {
Findpluginpackagename ();
return null;
}

@Override
protected 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 PackageInfo
String meta = null;
ApplicationInfo ainfo = null;
for (PackageInfo pi:list) {
String pkgname = pi.packagename;
try {
Ainfo = Pm.getapplicationinfo (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)) {
Determine if your plugin apk is the value of the meta
Mpluginpackagenamelist.add (Ainfo.packagename);
}
}
}
Must operate in the main thread
private void Swtichimguseplugin () {
I am here for convenience directly to take the first plug-in APK data, in fact, the user selected by the APK to switch
if (mpluginpackagenamelist.size ()! = 0) {
try {
Get plugin apk context by Createpackagecontext method
Context Otherpluscontext = Context.createpackagecontext (
Mpluginpackagenamelist.get (0),
Context.context_include_code
| context.context_ignore_security);
Get the Mainactvity class object by getting the plugin APK's context class loader, then
class<?> MainClass = Otherpluscontext
. getClassLoader ()
. LoadClass (Mpluginpackagenamelist.get (0) + ". Mainactivity ");
To get the drawable of the corresponding position by reflection
Method m = Mainclass.getmethod ("Getdrawableidforotherapp",
Int.class);
int imgbgid = (Integer) m.invoke (Mainclass.newinstance (), position);
In order to be able to cycle through 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 ();
}
}

}
}


Android Theme App Theme implementation

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.