Android如何依赖android:sharedUse…

Android给每个APK进程分配一个单独的用户空间,其manifest中的userid就是对应一个Linux用户 (Android 系统是基于Linux)的.
所以不同APK(用户)间互相访问数据默认是禁止的.
但是它也提供了2种APK间共享数据的形式:
1. Share Preference/Content Provider
APK可以指定接口和数据给任何其他APK读取. 需要自己实现接口和Share的数据.
2. Shared User id
通过Shared User id,拥有同一个User id的多个APK可以配置成运行在同一个进程中.所以默认就是
可以互相访问任意数据. 也可以配置成运行成不同的进程, 同时可以访问其他APK的数据目录下的
数据库和文件.就像访问本程序的数据一样.
比如某个公司开发了多个Android 程序, 那么可以把数据,图片等资源集中放到APK  A中去. 然后
这个公司的所有APK都使用同一个User ID, 那么所有的资源都可以从APK A中读取.
举个例子:
APK A 和APK B 都是C公司的产品,那么如果用户从APK A中登陆成功.那么打开APK B的时候就不用
再次登陆. 具体实现就是 A和B设置成同一个User ID:
    * 在2个APK的AndroidManifest.xml 配置User ID:
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.android.demo.a1"
    android:sharedUserId="com.c">
   这个"com.c" 就是user id, 然后packagename APK A就是上面的内容,  APK B可能
   是"com.android.demo.b1" 这个没有限制

这个设定好之后, APK B就可以像打开本地数据库那样 打开APK A中的数据库了.
APK A把登陆信息存放在A的数据目录下面. APK B每次启动的时候读取APK A下面的数据库
判断是否已经登陆:
APK B中的代码:
friendContext = this.createPackageContext( "com.android.demo.a1" , Context.CONTEXT_IGNORE_SECURITY);
通过A的package name 就可以得到A的 packagecontext.
通过这个context就可以直接打开数据库。
例子:
为了更好的UI体验,不错的App都会有几套不错的SKIN提供给用户来选择,以下是两个比较简单的工程,一个为主工程.apk,一个为皮肤.apk:
先看皮肤:
AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.tal.skin"
      android:versionCode="1"
      android:versionName="1.0"
      android:sharedUserId="com.tal.skinmain">①
    <uses-sdk android:minSdkVersion="7" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Skin"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

注释:① android:sharedUserId = “com.tal.skinmain” 通过Shared User id,拥有同一个User id的多个APK可以配置成运行在同一个进程中.所以默认就是可以互相访问任意数据. 也可以配置成运行成不同的进程, 同时可以访问其他APK的数据目录下的数据库和文件.就像访问本程序的数据一样. 

然后再皮肤工程里面放置两张图片,来显示替换皮肤样式。skin1.png,skin2.png
皮肤工程无需activity界面即可;
主工程:
AndroidMainfest.xml按照正常写法即可,无需配置什么东东,当时在设置皮肤的需要这样写上:
Activity.java
package com.tal.skinmain;
import com.tal.skin.R;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SkinMain extends Activity {

LinearLayout  linearLayout;
TextView textview;
Context ctx;
Button bt1,bt2,bt3;
boolean flag = true;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        textview = (TextView)findViewById(R.id.id_skin_textview);
        textview.setText("皮肤一");
        linearLayout = (LinearLayout)findViewById(R.id.id_skin_linearlayout);
       
        linearLayout.setBackgroundColor(Color.BLUE);
       
        try {
         //获取皮肤共享的数据包
         ctx = this.createPackageContext("com.tal.skin", CONTEXT_IGNORE_SECURITY);②
        } catch (NameNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
  }
  
  bt1 = (Button)findViewById(R.id.id_skin_bt1);
  bt2 = (Button)findViewById(R.id.id_skin_bt2);
  bt3 = (Button)findViewById(R.id.id_skin_bt3);
  bt1.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View v) {
    linearLayout.setBackgroundColor(Color.BLUE);
    textview.setText("默认皮肤");
   }});
  bt2.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View v) {
    linearLayout.setBackgroundDrawable(ctx.getResources().getDrawable(R.drawable.skin1)); 
    textview.setText("皮肤一");
   }});
  bt3.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View v) {
       linearLayout.setBackgroundDrawable(ctx.getResources().getDrawable(R.drawable.skin2));④
    textview.setText("皮肤二");
   }});
    }
}

注解:②③④获取皮肤工程的数据,当中有指定皮肤的包名
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/id_skin_linearlayout"
    >
<TextView
    android:id="@+id/id_skin_textview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<Button
   android:id="@+id/id_skin_bt1"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="默认"  
   />
    <Button
   android:id="@+id/id_skin_bt2"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="皮肤一"  
   />
    <Button
   android:id="@+id/id_skin_bt3"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="皮肤二"  
   />
</LinearLayout>

来源:
http://blog.163.com/dangzhengtao@yeah/blog/static/778008742011499562185/
http://wallage.blog.163.com/blog/static/17389624201011010539408/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值