码迷,mamicode.com
首页 > 其他好文 > 详细

插件化-皮肤

时间:2016-04-30 23:31:35      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:

1、清单文件

android:sharedUserId="com.zyh.skinplugin"

2、视图文件

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     android:id="@+id/ll"
 7     android:background="@drawable/spring_main_bg"
 8     tools:context=".MainActivity" >
 9 
10     <ImageButton
11         android:id="@+id/ib"
12         android:layout_width="wrap_content"
13         android:layout_height="wrap_content"
14         android:layout_gravity="right"
15         android:layout_marginRight="10dp"
16         android:src="@drawable/ic_launcher"
17         />
18 
19 </LinearLayout>
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6     <ListView 
 7         android:layout_width="wrap_content"
 8         android:layout_height="wrap_content"
 9         android:id="@+id/lv"
10         />
11 
12 </LinearLayout>

3、java文件

  1 package com.zyhitheima.plugin_main;
  2 
  3 import java.lang.reflect.Field;
  4 import java.util.ArrayList;
  5 import java.util.HashMap;
  6 import java.util.List;
  7 import java.util.Map;
  8 
  9 import dalvik.system.PathClassLoader;
 10 
 11 import android.os.Bundle;
 12 import android.app.Activity;
 13 import android.content.Context;
 14 import android.content.pm.PackageInfo;
 15 import android.content.pm.PackageManager;
 16 import android.content.pm.PackageManager.NameNotFoundException;
 17 import android.view.Gravity;
 18 import android.view.Menu;
 19 import android.view.View;
 20 import android.view.View.OnClickListener;
 21 import android.widget.AdapterView;
 22 import android.widget.AdapterView.OnItemClickListener;
 23 import android.widget.ImageButton;
 24 import android.widget.LinearLayout;
 25 import android.widget.ListView;
 26 import android.widget.PopupWindow;
 27 import android.widget.SimpleAdapter;
 28 import android.widget.Toast;
 29 
 30 public class MainActivity extends Activity implements OnClickListener, OnItemClickListener {
 31     private ImageButton ib;
 32     private LinearLayout ll;
 33     private List<Map<String, Object>> data;
 34     @Override
 35     protected void onCreate(Bundle savedInstanceState) {
 36         super.onCreate(savedInstanceState);
 37         setContentView(R.layout.activity_main);
 38         
 39         ib = (ImageButton) findViewById(R.id.ib);
 40         ll = (LinearLayout) findViewById(R.id.ll);
 41         
 42         data = new ArrayList<Map<String, Object>>();
 43         
 44         ib.setOnClickListener(this);
 45         
 46         
 47     }
 48     @Override
 49     public void onClick(View v) {
 50         View view = View.inflate(this, R.layout.popup_window, null);
 51         ListView lv = (ListView) view.findViewById(R.id.lv);
 52         
 53         PopupWindow popupWindow = new PopupWindow(this);
 54         popupWindow.setContentView(view);
 55         popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.pw_bg));
 56         popupWindow.setFocusable(true);//设置焦点
 57         
 58         int[] location = new int[2];
 59         ib.getLocationInWindow(location);//获取ib的坐标
 60         
 61         //搜索插件
 62         data = findPlugins();
 63         
 64         if(data.size() == 0){
 65             Toast.makeText(this, "hello world", 0).show();
 66             return;
 67         }
 68         
 69         SimpleAdapter adapter = new SimpleAdapter(this, data, android.R.layout.simple_list_item_1, new String[]{"label"}, new int[]{android.R.id.text1});
 70         lv.setAdapter(adapter);
 71         
 72         lv.setOnItemClickListener(this);
 73         
 74         popupWindow.setWidth(100);
 75         popupWindow.setHeight(data.size() * 40);
 76         popupWindow.showAtLocation(ib, Gravity.LEFT|Gravity.TOP, location[0], location[1]+ib.getHeight());
 77     }
 78     private List<Map<String, Object>> findPlugins() {
 79         List<Map<String, Object>> plugins = new ArrayList<Map<String, Object>>();
 80         //包管理器
 81         PackageManager pm = this.getPackageManager();
 82         List<PackageInfo> installPackages = pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);//获取所有安装的package包
 83         for(PackageInfo info : installPackages){
 84             String pkgName = info.packageName;
 85             String sharedUserId = info.sharedUserId;
 86             if(sharedUserId == null || !sharedUserId.equals("com.zyh.skinplugin") || pkgName.endsWith(this.getPackageName())){
 87                 continue;
 88             }
 89             
 90             String label = (String) pm.getApplicationLabel(info.applicationInfo);
 91             
 92             Map<String, Object> map = new HashMap<String, Object>();
 93             map.put("label", label);
 94             map.put("pkgName", pkgName);
 95             plugins.add(map);
 96         }
 97         return plugins;
 98     }
 99     @Override
100     public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
101         String pkgName = (String) data.get(position).get("pkgName");
102         Context plugContext = null;
103         try {
104             plugContext = this.createPackageContext(pkgName, CONTEXT_IGNORE_SECURITY|CONTEXT_INCLUDE_CODE);
105         } catch (Exception e) {
106             // TODO Auto-generated catch block
107             e.printStackTrace();
108         }
109         
110         try {
111             int bgId = getBgIdByPkgName(pkgName, plugContext);
112             //ll.setBackgroundDrawable(plugContext.getResources().getDrawable(bgId));
113             ll.setBackground(plugContext.getResources().getDrawable(bgId));
114         } catch (Exception e) {
115             // TODO Auto-generated catch block
116             e.printStackTrace();
117         }
118     }
119     private int getBgIdByPkgName(String pkgName, Context plugContext) throws Exception{
120         PathClassLoader pathClassLoader = new PathClassLoader(plugContext.getPackageResourcePath(), ClassLoader.getSystemClassLoader());
121         Class<?> forName = Class.forName(pkgName + ".R$drawable",true,pathClassLoader);
122         Field[] declaredFields = forName.getDeclaredFields();
123         for(Field field : declaredFields){
124             if(field.getName().contains("main_bg")){
125                 int index = 0;
126                 index = field.getInt(R.drawable.class);
127                 return index;
128             }
129         }
130         return 0;
131     }
132 
133 }

 

4、测试应用即插件的清单配置

1   <activity
2             android:name="com.zyhitheima.plugin_sumer.MainActivity"
3             android:label="@string/app_name" >
4             <intent-filter>
5                 <action android:name="com.zyhitheima.plugin_sumer" />
6 
7                 <category android:name="android.intent.category.DEFAULT" />
8             </intent-filter>
9         </activity>

注意它的action 是包名,category使用default

插件化-皮肤

标签:

原文地址:http://www.cnblogs.com/zhongyinghe/p/5449613.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!