Interprocess Communication时是否可以获取Activity的调用者应用程序的包名?

Is it possible to get the package name of Activities' caller app when Interprocess Communication?

我有一个 Activity A,可以在我的应用程序中被 Application1Application2 调用。

Manifest 文件中 AcitivityA 的配置如下:

<activity
   android :name=".activity.ActivityA"
   android :exported="true"
   android :screenOrientation="portrait"/>

Application1Application2如何调用ActivityA:

PackageManager packageManager = getPackageManager();
Intent intent = new Intent();
intent.setClassName(“com.abc.test", “com.abc.test.activity.ActivityA" );
List activities = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
if (activities.size() > 0 ) {
   startActivity(intent);
}

为了安全,我想在ActivityA添加一个包含Application1Application2的包名的白名单,只有[=]的包名才能打开23=]来电者在白名单中。

那么是否可以在ActivityA中获取Activities'调用程序的包名?

已编辑:

如果没有 Intent 中的额外数据是不可能的,还有其他保持 Activity A 安全的想法吗?

只要调用方不在extra中提供自己的信息,答案是否定的

如果您想要确保只允许特定的呼叫者,请使用权限。 参见

如果您拥有所有呼叫者,那么我建议使用相同的密钥对所有这些进行签名,并使用 protectionLevel Signature.

的权限

另一种需要更多代码的方法是使用 services 进行通信。

引用第一条link:

Using Binder or Messenger is the preferred mechanism for RPC-style IPC in Android. They provide a well-defined interface that enables mutual authentication of the endpoints, if required.

我所做的是通过意图传递调用 activity。例如

public class MainActivity extends AppCompatActivity {
    private final static String THIS_ACTIVITY = "MainActivity";
.........

    Intent intent = new Intent(this, ShopAddActivity.class);
    intent.putExtra("Caller",THIS_ACTIVITY);
    startActivity(intent);

在另一个 activity 中(调用相同的 child activity):-

public class ShopListByCursorActivity extends AppCompatActivity {

    private final static String THIS_ACTIVITY = "ShopListByCursorActivity";

.......

    Intent intent = new Intent(findViewById(R.id.aslbclv01).getContext(), ShopAddActivity.class);
    intent.putExtra("Caller", THIS_ACTIVITY + "Update");
    startActivity(intent);

然后在被叫activity(作为检测来电的一个例子)

if(getIntent().getStringExtra("Caller").equals("ShopListByCursorActivityUpdate")) {

      DO STUFF HERE
        }

尝试 ActivitygetCallingPackage() and getCallingActivity() 方法。