Android theme application theme implementation, androidtheme

Source: Internet
Author: User

Android theme application theme implementation, androidtheme
The Android app Theme is implemented through apk. The following is a demo.

 
1. First, you must create an apk plug-in similar to the apk, and then add a meta-data to the application of the AndroidManifest. xml file of the apk. This is the next step to find whether the apk of your plug-in is marked.

<Meta-data android: name = "skin_demo2_plugin" android: value = "com. example. skindemo2.icon"/>

2. Add some images to the apk or use other images (including style) for demonstration. Define a method in the MainActivity of the apk. This method is mainly used to facilitate the quick call of the primary apk.

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. Read all installed applications in the primary apk, analyze the meta-data of applicationInfo, and determine whether the plug-in apk is your own.
 
Then, use context. createPackageContext (packageName, int flag) to obtain the context of the corresponding plug-in. Then, the class of MainActivity is obtained through the class loader, and the value returned by the method is obtained through reflection. Then we can 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 );
}
Cmd.exe cute ();

}

Private void findPluginPackageName (){
PackageManager pm = context. getPackageManager ();
List <PackageInfo> list = pm. getInstalledPackages (0); // obtain PackageInfo for all installed apk files
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 whether the plug-in apk is its own by using the meta Value
MPluginPackageNameList. add (aInfo. packageName );
}
}
}
// Operations must be performed in the main thread
Private void swtichImgUsePlugin (){
// Here, I directly retrieve the first plug-in apk data for convenience. In reality, it is switched through the apk selected by the user.
If (mPluginPackageNameList. size ()! = 0 ){
Try {
// Use the createPackageContext method to obtain the context of the plug-in apk
Context otherplusContext = context. createPackageContext (
MPluginPackageNameList. get (0 ),
Context. CONTEXT_INCLUDE_CODE
| Context. CONTEXT_IGNORE_SECURITY );
// Obtain the context class loader of the plug-in apk and obtain the MainActvity class object.
Class <?> MainClass = otherplusContext
. GetClassLoader ()
. LoadClass (mPluginPackageNameList. get (0) + ". MainActivity ");
// Obtain the drawable at the corresponding position through reflection
Method m = mainClass. getMethod ("getDrawableIdForOtherApp ",
Int. class );
Int imgBgId = (Integer) m. invoke (mainClass. newInstance (), position );
// To switch img cyclically
If (imgBgId = 0 ){
Position = 0;
} Else {
Position + = 1;
}
// Obtain the drawable object
Drawable drawable = otherplusContext. getResources ()
. GetDrawable (imgBgId );
MShowImg. setImageDrawable (drawable );
} Catch (Exception e ){
E. printStackTrace ();
}
}

}
}


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.