Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1062251
  • 博文数量: 403
  • 博客积分: 10272
  • 博客等级: 上将
  • 技术积分: 4407
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 14:22
文章分类

全部博文(403)

文章存档

2012年(403)

分类: 嵌入式

2012-03-17 20:53:10

如何让两个程序贡献资源的实现方式:共享进程id

步骤:1: 共享进程id方式
           2:利用反射机制,加载class, 对类进行操作

           3:交叉编译,安装。

共享资源可以做到什么呢?

1.可做到换肤,换主题的功能。主题和皮肤可以独立于一个apk

2.还可以做什么?请广大朋友给列几个,呵呵

一下是这个主程序的代码实例,其他的程序只要是和此程序有一个共享的进程id就可以实现。

在清单文件manifest中定义:

  1. android:sharedUserId="share.scl"  

  1. ""  
  2.     package="com.share.main"  
  3.     android:versionCode="1"  
  4.     android:sharedUserId="share.scl"  
  5.     android:versionName="1.0" >  
  6.   
  7.     "10" />  
  8.   
  9.     
  10.         android:icon="@drawable/ic_launcher"  
  11.         android:label="@string/app_name" >  
  12.         
  13.             android:name=".ShareMainActivity"  
  14.             android:label="@string/app_name" >  
  15.               
  16.                 "android.intent.action.MAIN" />  
  17.   
  18.                 "android.intent.category.LAUNCHER" />  
  19.               
  20.           
  21.       
  22.   
  23.   


  1. package com.share.main;  
  2.   
  3. import java.lang.reflect.Field;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.Context;  
  7. import android.content.pm.PackageManager.NameNotFoundException;  
  8. import android.graphics.drawable.Drawable;  
  9. import android.os.Bundle;  
  10. import android.os.Handler;  
  11. import android.os.Message;  
  12. import android.util.Log;  
  13. import android.view.View;  
  14. import android.view.View.OnClickListener;  
  15. import android.widget.Button;  
  16. import android.widget.TextView;  
  17.   
  18. public class ShareMainActivity extends Activity implements OnClickListener{  
  19.     /** Called when the activity is first created. */  
  20.     private static final String TAG = ShareMainActivity.class.getSimpleName();  
  21.     Button button;  
  22.     Handler mHandler = new Handler(){  
  23.   
  24.         @Override  
  25.         public void handleMessage(Message msg) {  
  26.             button.setBackgroundDrawable((Drawable)msg.obj);  
  27.             button.setText(null);  
  28.             TextView text = (TextView) findViewById(R.id.text);  
  29.             text.setText(getResources().getString(R.string.hellp2));  
  30.         }  
  31.           
  32.     };  
  33.     @Override  
  34.     public void onCreate(Bundle savedInstanceState) {  
  35.         super.onCreate(savedInstanceState);  
  36.         setContentView(R.layout.main);  
  37.         button = (Button) findViewById(R.id.button1);  
  38.         button.setOnClickListener(this);  
  39.     }  
  40.   
  41.     public void onClick(View v) {  
  42.         if(v == button){  
  43.             new Thread(){  
  44.   
  45.                 @Override  
  46.                 public void run() {  
  47.                     getOtherAppRes();  
  48.                 }  
  49.             }.start();  
  50.         }  
  51.     }  
  52.     private void getOtherAppRes(){  
  53.         try {  
  54.             //Context.CONTEXT_IGNORE_SECURITY:取消任何安全限制。  
  55.             //Context.CONTEXT_INCLUDE_CODE:允许此程序加载其他程序的代码。  
  56.             Context ontherContext = createPackageContext("com.android.test.login", Context.CONTEXT_IGNORE_SECURITY|Context.CONTEXT_INCLUDE_CODE);  
  57.             Class rClass =  ontherContext.getClassLoader().loadClass("com.android.test.login.R");  
  58.               
  59.             Class[] classes = rClass.getClasses();  
  60.             int picId = 0;  
  61.             for(int i = 0; i
  62.                 Class c = classes[i];  
  63.                 Log.i(TAG, "class name:" + c.getSimpleName() + "-------------");  
  64.                 Field[] fes = c.getFields();  
  65.                 for(int j = 0; j < fes.length; j++){  
  66.                     Field f = fes[j];  
  67.                     String fName = f.getName();  
  68.                     Log.i(TAG, "field name:" + fName + "***********************" );  
  69.                     if("h001".equals(fName)){  
  70.                         picId = f.getInt(fName);  
  71.                     }  
  72.                       
  73.                 }  
  74.             }  
  75.             if(picId != 0){  
  76.                 Message msg = new Message();  
  77.                 msg.obj = ontherContext.getResources().getDrawable(picId);  
  78.                 mHandler.sendMessage(msg);  
  79.             }  
  80.             Log.i(TAG, "finish__________________");  
  81.         } catch (NameNotFoundException e) {  
  82.               
  83.         } catch (ClassNotFoundException e) {  
  84.               
  85.         } catch (IllegalArgumentException e) {  
  86.         } catch (IllegalAcces***ception e) {  
  87.         }  
  88.     }  
  89. }  

阅读(917) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~