2

它是关于在同一进程中运行的不同应用程序。我读过他们共享一个用户 ID 并使用相同的证书签名。

我的问题是:

  1. 用户 ID在这里定义为:

    用户 ID (UID) 是类 Unix 操作系统分配给每个用户的唯一正整数。每个用户都通过其 UID 向系统标识,并且用户名通常仅用作人类的接口。

现在既然用户是安装在linux系统上的应用程序,那么user id就是该应用程序在linux系统中的唯一标识。

那么两个应用程序如何共享相同的用户ID。此外,如果由不同开发人员开发的两个应用程序曾经共享相同的用户 ID 怎么办?

  1. 我在这里读到:

    Android 系统要求所有已安装的应用程序都使用证书进行数字签名,证书的私钥由应用程序的开发人员持有。Android 系统使用证书作为识别应用程序作者和建立应用程序之间信任关系的一种手段。

那么不同的应用程序,比如说由不同的开发者开发的,如何使用相同的证书进行签名呢?

3. 谁能给我一个例子,不同应用程序的组件在同一个进程中运行?

先感谢您。

4

1 回答 1

-1
package com.pluralsight.example.ShareApp1;

import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.TextView;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ShareApp1Activity extends Activity {
    private static final String         LOG_TAG = "ShareApp1Activity";
    private static final String         SHARE_APP2_PKG_NAME = "com.pluralsight.example.ShareApp2";
    private static final String         DATA_DIR_FILES_DIR = "/files";
    private static final String         SHARE_APP2_DATA_FILE = "data.txt";

    private PackageManager              mPM;
    private TextView                    mApp2Path;
    private TextView                    mApp2Data;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //  Get the package manager, we will use it to find the app info
        //  for our other package.
        mPM = getPackageManager();

        //  Get the UI components we need to set as part of our activity.
        //  This is going to include a text field showing where the other
        //  app is installed as well as a graphic component found in the
        //  other package.
        mApp2Path = (TextView)findViewById(R.id.share2_install_path);
        mApp2Data = (TextView)findViewById(R.id.data_file);
    }

    @Override
    public void onStart() {
        String                  path = null;
        String                  data = null;

        super.onStart();

        //  Use the PackageManager to retrieve the details for our other
        //  package.
        try {
            ApplicationInfo ai =
                mPM.getApplicationInfo(SHARE_APP2_PKG_NAME,
                                       PackageManager.GET_META_DATA);
            path = ai.dataDir;
            path += DATA_DIR_FILES_DIR;
            File inFile = new File(path, SHARE_APP2_DATA_FILE);
            FileInputStream in = new FileInputStream(inFile);
            int len = (int)inFile.length();
            if (len == 0) {
                in.close();
                throw new IOException();
            }

            byte[] dataBytes = new byte[len];
            in.read(dataBytes, 0, len);
            in.close();
            data = new String(dataBytes);
        } catch (PackageManager.NameNotFoundException e) {
            path = getString(R.string.app2_not_found);
        } catch (FileNotFoundException e2) {
            data = getString(R.string.no_data_file);
        } catch (IOException e3) {
            data = getString(R.string.no_data);
        }

        mApp2Path.setText(path);
        mApp2Data.setText(data);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.pluralsight.example.ShareApp1"
          android:versionCode="1"
          android:versionName="1.0"
          android:sharedUserId="pluralsight.example.shareId">
    <uses-sdk android:minSdkVersion="14"/>
    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
        <activity android:name="ShareApp1Activity"
                  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>

package com.pluralsight.example.ShareApp2;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class ShareApp2Activity extends Activity {
    private static final String        DATA_FILE_NAME = "data.txt";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    public void onStart() {
        FileOutputStream out = null;

        super.onStart();

        //  Write out a simple data field.
        String dataStr = getString(R.string.data);
        try {
            out = openFileOutput(DATA_FILE_NAME, 0);
            out.write(dataStr.getBytes(), 0, dataStr.length());
            out.close();
        } catch (FileNotFoundException e) {
            Toast.makeText(this, getString(R.string.nofile), Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            Toast.makeText(this, getString(R.string.ioerr), Toast.LENGTH_LONG).show();
            finish();
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e2) {
                    //  ignore
                }
            }

            return;
        }

        Toast.makeText(this, getString(R.string.now_load_1), Toast.LENGTH_LONG).show();
        finish();
    }
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.pluralsight.example.ShareApp2"
          android:versionCode="1"
          android:versionName="1.0"
          android:sharedUserId="pluralsight.example.shareId">
    <uses-sdk android:minSdkVersion="14"/>
    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
        <activity android:name="ShareApp2Activity"
                  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>
于 2015-05-17T11:33:32.663 回答