两个应用程序共享用户名应用程序、用户名、两个

2023-09-05 07:20:41 作者:許丅的承诺,

可以在两个应用程序共享的用户ID访问诸如可绘制或串彼此的资源?

Can two app with shared UserID access each other resources like drawables or strings?

他们可以访问对方的资产?

Can they access each other assets?

他们可以启用或其它禁用成分?

Can they enable or disable components of the other?

如果上述任何可能,请解释一下应该怎样做。

If any of these is possible please explain how it must be done.

我搜索了很多,但无法找到用户id共享任何的例子。

I searched a lot but couldn't find any example about userId sharing.

推荐答案

您可以使用安卓sharedUserId 在AndroidManifest.xml中,让您的应用程序共享相同的用户ID另一个应用程序。

You can use android:sharedUserId in AndroidManifest.xml to let your application share the same user id with another application.

机器人:sharedUserId

一个Linux用户ID,将与其他共享的名称   应用程序。默认情况下,Android的分配自己的每一个应用程序   唯一的用户ID。但是,如果该属性被设置为相同的值   为两个或更多的应用程序,它们都将共享相同的ID -   只要它们也由相同的证书进行签名。   如果使用相同的用户ID应用程序可以访问对方的数据,   需要的话,在同一进程中运行。

The name of a Linux user ID that will be shared with other applications. By default, Android assigns each application its own unique user ID. However, if this attribute is set to the same value for two or more applications, they will all share the same ID — provided that they are also signed by the same certificate. Application with the same user ID can access each other's data and, if desired, run in the same process.

请注意,他们需要通过同一证书进行签名。

Notice that they need to be signed by the same certificate.

两个应用程序共享同一个用户ID可以访问对方的资源。

Two applications share the same user id may access each other's resource.

例如:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.shareusertesta"
    android:versionCode="1"
    android:versionName="1.0" 
    android:sharedUserId="com.example">

然后我们就可以通过初始化的 com.example 一个新的上下文:

Context friendContext = this.createPackageContext( "com.example",Context.CONTEXT_IGNORE_SECURITY);

和访问应用程序的一些资源:

And access some resources of that application:

friendContext.getResources().getString(id);
friendContext.getResources().getDrawable(id);
friendContext.registerReceiver(...);