Return the name of the package that invoked this activity. This is who the data in setResult() will be sent to. You can use this information to validate that the recipient is allowed to receive the data. Note: if the calling activity is not expecting a result (that is it did not use the startActivityForResult form that includes a request code), then the calling package will be null. Note: prior to Build.VERSION_CODES.JELLY_BEAN_MR2, the result from this method was unstable. If the process hosting the calling package was no longer running, it would return null instead of the proper package name. You can use getCallingActivity() and retrieve the package name from that instead.解释一下
时间: 2024-04-22 16:23:43 浏览: 88
这段英文是Android官方文档中对于getCallingPackage()方法的解释。它的意思是:返回调用此活动的包的名称。这是要将setResult()中的数据发送到的对象。您可以使用此信息来验证接收方是否有权接收数据。注意:如果调用活动不需要结果(即未使用包含请求代码的startActivityForResult形式),则调用包将为null。注意:在Build.VERSION_CODES.JELLY_BEAN_MR2之前,此方法的结果不稳定。如果托管调用包的进程不再运行,则返回null而不是正确的包名称。您可以使用getCallingActivity()并从中检索包名称。
简单来说,这段话告诉我们,getCallingPackage()方法可以返回调用当前Activity的应用程序的包名,我们可以使用它来验证接收方是否有权接收数据。但是在Android 4.3之前,该方法的结果可能不稳定,如果调用方的进程已经停止运行,它可能会返回null,此时我们可以使用getCallingActivity()方法来获取包名。
阅读全文