zoukankan      html  css  js  c++  java
  • 初学Android ShareUserId(多应用共享资源和服务)

    Android里面每个app都有一个唯一的linux user ID。我们通过SharedUserId,让使用相同的userID的两个app应用可以看到对方的文件。为了节省资源,具有相同ID的apk也可以在相同的linux进程中进行。ShareUserId的作用:数据共享、调用其他程序资源。

    步骤:

    一、新建a,b两个android项目,a  的包名com.rainwii.client     b  的包名com.rainwii.service

    二、在a、b项目向的manifest.xml增加相同的android:sharedUserId="com.rainwii.share"。

    a manifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.rainwii.client"
        android:versionCode="1"
        android:versionName="1.0"
        android:sharedUserId="com.rainwii.share" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="18" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.rainwii.client.MainActivity"
                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>

    b manifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.rainwii.service"
        android:versionCode="1"
        android:versionName="1.0" 
        android:sharedUserId="com.rainwii.share">
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="18" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.rainwii.service.MainActivity"
                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>

    三、在MainActivity.class中利用contextcreatePackageContext获取另外一个程序的Context。

    a的MainActivity.class

    package com.rainwii.client;
    
    import com.rainwii.main.R;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Context;
    import android.content.pm.PackageManager.NameNotFoundException;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    Button startappbutton;
    String str;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            try {
                Context ct=this.createPackageContext ("com.rainwii.service", Context.CONTEXT_IGNORE_SECURITY);
               str = ct.getString(R.string.app_name);
               
            } catch (NameNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            startappbutton= (Button) findViewById(R.id.button1);
            startappbutton.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(MainActivity.this, ""+str, Toast.LENGTH_SHORT).show();
                }
            });
                  
          
        }
    
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
        
    }

    b的MainActivity.class

    package com.rainwii.service;
    
    import com.rainwii.main.R;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Context;
    import android.content.pm.PackageManager.NameNotFoundException;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    Button startappbutton;
    String str;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            try {          
                Context ct=this.createPackageContext ("com.rainwii.client", Context.CONTEXT_IGNORE_SECURITY);
               str = ct.getString(R.string.app_name);
               
            } catch (NameNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            startappbutton= (Button) findViewById(R.id.button1);
            startappbutton.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(MainActivity.this, ""+str, Toast.LENGTH_SHORT).show();
                }
            });
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
        
    }

    注释:Context有个createPackageContext(packageName,flags)方法,可以创建另外一个包的上下文,这个实例不同于它本身的Context实例,但是功能是一样的。这个方法有两个参数:  1、packageName 包名,要得到Context的包名

        2、flags 标志位,有CONTEXT_INCLUDE_CODE和CONTEXT_IGNORE_SECURITY两个选项。CONTEXT_INCLUDE_CODE的意思是包括代码,也就是说可以执行这个包里面的代码。CONTEXT_IGNORE_SECURITY的意思 是忽略安全警告,如果不加这个标志的话,有些功能是用不了的,会出现安全警告。

                              作者:xubuhang                出处:http://www.cnblogs.com/xubuhang/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 

     
查看全文
  • 相关阅读:
    使用 Dockerfile 自定义 Nginx 镜像
    Windows下Rancher复制Pod内文件到本地
    SSL基础知识及Nginx/Tomcat配置SSL
    linux内核源码
    strace:跟踪进程的系统调用 、ltrace:跟踪进程调用库函数,runlevel & init & service
    C10K,C10M问题
    操作系统
    深入理解GOT表和PLT表
    为什么 List<Struct> 在 C# 中的分配速度比 List<Class> 快 15 倍
    如何计算时间复杂度
  • 原文地址:https://www.cnblogs.com/xubuhang/p/4133466.html
  • Copyright © 2011-2022 走看看