Android app Theme (2)

Source: Internet
Author: User

The second method to implement Theme of the android app 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.

 
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
 
  
MPluginPackageNameList = new ArrayList
  
   
(); Private 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
   
    
() {@ Overrideprotected Void doInBackground (Void... params) {findPluginPackageName (); return null ;}@ Overrideprotected void onPostExecute (Void result) implements cute ();} private void findPluginPackageName () {PackageManager pm = context. getPackageManager (); List
    
     
List = pm. getInstalledPackages (0); // obtain PackageInfoString meta = null for all installed apk packages; 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) {// use the meta value to determine whether the plug-in is apkmPluginPackageNameList. add (aInfo. packageName) ;}}// private void swtichImgUsePlugin () must be operated in the main thread {// I directly retrieve the first plug-in apk data for convenience, in practice, you can switch if (mPluginPackageNameList. size ()! = 0) {try {// use the createPackageContext method to obtain the contextContext otherplusContext = context of the plug-in apk. createPackageContext (mPluginPackageNameList. get (0), Context. CONTEXT_INCLUDE_CODE | Context. CONTEXT_IGNORE_SECURITY); // obtain the context Class loader of the plug-in apk, and then obtain the Class Object Class of MainActvity.
     MainClass = otherplusContext. getClassLoader (). loadClass (mPluginPackageNameList. get (0) + ". mainActivity "); // obtain the drawableMethod m = mainClass at the corresponding position through reflection. getMethod ("getDrawableIdForOtherApp", int. class); int imgBgId = (Integer) m. invoke (mainClass. newInstance (), position); // In order to cyclically switch img 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.