Android – 在创建显式 Intent 时,您在构造函数中究竟传递了什么?

huangapple go评论59阅读模式
英文:

Android - What are exactly are you passing to the constructor of an intent when creating an explicit intent

问题

在创建 Android 中的显式 Intent 时,您会传递一个 packageContext 对象和一个 Class 对象。关于 packageContext,我发现您传递的是“this”,它是对调用该方法的当前对象的引用,以便 Intent 知道它是从该类传递的,假设您没有插入“this”,那么这会对 Intent 产生什么影响?为什么它不像第二个对象一样是一个 Class 对象,而是一个 packageContext 对象?那么 packageContext 和 Class 对象究竟是什么?

英文:

When you are creating an explicit intent in android you pass a packageContext object and a Class object. For the packageContext what I found out is that you pass "this" which is a reference to the current object calling the method so that the intent knows that it is being passed from that class, assuming you did't insert "this" what changes would that have on the intent and why is it also not a Class object like the second one and a packageContect object instead and what exactly is a packageContext and Class object?

答案1

得分: 1

"this" 只表示继承了 Context 类的类的对象。在创建显式意图对象时,您不能在任何地方都使用 "this"。例如,在匿名类内部创建显式意图时,您不会使用 "this"。如果您必须为特定组件(如活动或服务)创建意图,则必须传递上下文对象;否则,这是不必要的,您可以使用其他可能的构造函数。例如,特定组件的示例包括您的意图将前往的活动,或者您特别想要启动的服务。

Packagecontext:实现此类的应用程序包的上下文。
Context:有关应用程序环境的全局信息的接口。

英文:

"this" only represents the object of a class which has inherited Context class. You cannot use this everywhere while creating an explicit intent object. For e.g. inside an anonymous class, while creating an explicit intent, you wouldn't use "this". If you have to create an intent for a specific component like activity or a service, you got to pass the context object else, its not required and you may use other possible constructor.e.g. of the specific component include an activity where your intent would take you or a service which you specifically wanna start.

Packagecontext: A Context of the application package implementing this class.
Context : Interface to global information about an application environment

huangapple
  • 本文由 发表于 2020年10月5日 23:26:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/64211618.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定