PendingIntent详解
Posted siwnchh
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PendingIntent详解相关的知识,希望对你有一定的参考价值。
public static PendingIntent getActivity (Context context, int requestCode, Intent intent, int flags)
Retrieve a PendingIntent that will start a new activity, like calling Context.startActivity(Intent)
.
Note that the activity will be started outside of the context of an existing activity, so you must use the Intent.FLAG_ACTIVITY_NEW_TASK
launch
flag in the Intent.
因为对于Context的startActivity方法,如果不是在其子类(Activity)中调用,那么必须对Intent加上FLAG_ACTIVITY_NEW_TASK。
Parameters
context | The Context in which this PendingIntent should start the activity. |
---|---|
requestCode | Private request code for the sender (currently not used). |
intent | Intent of the activity to be launched. |
flags |
May be FLAG_ONE_SHOT , FLAG_NO_CREATE , FLAG_CANCEL_CURRENT , FLAG_UPDATE_CURRENT ,
or any of the flags as supported byIntent.fillIn() to
control which unspecified parts of the intent that can be supplied when the actual send happens.当我们使用Intent.fillIn()方法时,表示其Intent的某个数据项可以被send方法的Inent参数进行覆盖重写。 |
Returns
FLAG_NO_CREATE
has
been supplied.public static PendingIntent getActivities (Context context, int requestCode, Intent[] intents, int flags)
Like getActivity(Context,
int, Intent, int)
, but allows an array of Intents to be supplied. The first Intent in the array is taken as the primary key for the PendingIntent, like the single Intent given to getActivity(Context,
int, Intent, int)
. Upon sending the resulting PendingIntent, all of the Intents are started in the same way as they would be by passing them to startActivities(Intent[])
.
The first intent in the array will be started outside of the context of an existing activity, so you must use the Intent.FLAG_ACTIVITY_NEW_TASK
launch
flag in the Intent. (Activities after the first in the array are started in the context of the previous activity in the array, so FLAG_ACTIVITY_NEW_TASK is not needed nor desired for them.)
The last intent in the array represents the key for the PendingIntent. In other words, it is the significant element for matching (as done with the single intent given togetActivity(Context,
int, Intent, int)
, its content will be the subject of replacement by send(Context,
int, Intent)
andFLAG_UPDATE_CURRENT
,
etc. This is because it is the most specific of the supplied intents, and the UI the user actually sees when the intents are started.
Parameters
context | The Context in which this PendingIntent should start the activity. |
---|---|
requestCode | Private request code for the sender (currently not used). |
intents | Array of Intents of the activities to be launched. |
flags |
May be FLAG_ONE_SHOT , FLAG_NO_CREATE , FLAG_CANCEL_CURRENT , FLAG_UPDATE_CURRENT ,
or any of the flags as supported byIntent.fillIn() to
control which unspecified parts of the intent that can be supplied when the actual send happens.当我们使用Inent.fillIn()所支持的flags时,表示其Intent的数据项可以被send方法的Intent参数覆盖重写。 |
Returns
-
Returns an existing or new PendingIntent matching the given parameters. May return null only if
FLAG_NO_CREATE
has been supplied.
public static PendingIntent getService (Context context, int requestCode, Intent intent, int flags)
Retrieve a PendingIntent that will start a service, like calling Context.startService()
.
The start arguments given to the service will come from the extras of the Intent.
Parameters
context | The Context in which this PendingIntent should start the service. |
---|---|
requestCode | Private request code for the sender (currently not used). |
intent | An Intent describing the service to be started. |
flags |
May be FLAG_ONE_SHOT , FLAG_NO_CREATE , FLAG_CANCEL_CURRENT , FLAG_UPDATE_CURRENT ,
or any of the flags as supported byIntent.fillIn() to
control which unspecified parts of the intent that can be supplied when the actual send happens.当我们使用Intent.fillIn()方法的flags时,它表示其Intent的数据项可以被send方法的Intent参数覆盖重写。 |
Returns
-
Returns an existing or new PendingIntent matching the given parameters. May return null only if
FLAG_NO_CREATE
has been supplied.
public static PendingIntent getBroadcast (Context context, int requestCode, Intent intent, int flags)
Retrieve a PendingIntent that will perform a broadcast, like calling Context.sendBroadcast()
.
Parameters
context | The Context in which this PendingIntent should perform the broadcast. |
---|---|
requestCode | Private request code for the sender (currently not used). |
intent | The Intent to be broadcast. |
flags |
May be FLAG_ONE_SHOT , FLAG_NO_CREATE , FLAG_CANCEL_CURRENT , FLAG_UPDATE_CURRENT ,
or any of the flags as supported byIntent.fillIn() to
control which unspecified parts of the intent that can be supplied when the actual send happens.当我们使用Intent.fillIn()方法的flags时,它表示其Intent的数据项可以被send方法的Intent参数覆盖重写。 |
Returns
-
Returns an existing or new PendingIntent matching the given parameters. May return null only if
FLAG_NO_CREATE
has been supplied.
public void send ()
Perform the operation associated with this PendingIntent.
Throws
PendingIntent.CanceledException |
Throws CanceledException if the PendingIntent is no longer allowing more intents to be sent through it. |
---|
public void send (Context context, int code, Intent intent)
Perform the operation associated with this PendingIntent, allowing the caller to specify information about the Intent to use.
Parameters
context |
The Context of the caller. 该参数是因为intent参数才需要提供的,所用如果你的intent参数不为null的话,该参数也不能为null. |
---|---|
code | Result code to supply back to the PendingIntent‘s target. |
intent |
Additional Intent data. See Intent.fillIn() for
information on how this is applied to the original Intent. |
Throws
PendingIntent.CanceledException | Throws CanceledException if the PendingIntent is no longer allowing more intents to be sent through it. |
---|
public void send (int code, PendingIntent.OnFinished onFinished, Handler handler)
Perform the operation associated with this PendingIntent, allowing the caller to be notified when the send has completed.
Parameters
code | Result code to supply back to the PendingIntent‘s target. |
---|---|
onFinished |
The object to call back on when the send has completed, or null for no callback. 通过该参数,我们可以设置在Intent发送成功后的回调函数。 |
handler |
Handler identifying the thread on which the callback should happen. If null, the callback will happen from the thread pool of the process. 用于说明onFinished参数指定的回调函数,最终在哪个Handler中进行调用。 |
Throws
PendingIntent.CanceledException | Throws CanceledException if the PendingIntent is no longer allowing more intents to be sent through it. |
---|
public void send (Context context, int code, Intent intent, PendingIntent.OnFinished onFinished, Handler handler)
Perform the operation associated with this PendingIntent, allowing the caller to specify information about the Intent to use and be notified when the send has completed.
For the intent parameter, a PendingIntent often has restrictions on which fields can be supplied here, based on how the PendingIntent was retrieved ingetActivity(Context,
int, Intent, int)
, getBroadcast(Context,
int, Intent, int)
, or getService(Context,
int, Intent, int)
.
Parameters
context |
The Context of the caller. This may be null if intent is also null. 该参数是因为intent参数才需要提供的,一般是当前的context,如果你的intent参数不为null的话,该函数也不能为null. |
---|---|
code | Result code to supply back to the PendingIntent‘s target. |
intent |
Additional Intent data. See Intent.fillIn() for
information on how this is applied to the original Intent. Use null to not modify the original Intent. |
onFinished |
The object to call back on when the send has completed, or null for no callback. 通过该参数,我们可以指定Intent发送成功后的回调函数。 |
handler |
Handler identifying the thread on which the callback should happen. If null, the callback will happen from the thread pool of the process. 该参数说明onFinished参数指定的回调函数将在哪个Handler中进行调用。 |
Throws
PendingIntent.CanceledException | Throws CanceledException if the PendingIntent is no longer allowing more intents to be sent through it. |
---|
public void cancel ()
Cancel a currently active PendingIntent. Only the original application owning an PendingIntent can cancel it.
只有PengdingIntent的原应用程序才能调用cancel()来把它从系统中移除掉。
public String getTargetPackage ()
Return the package name of the application that created this PendingIntent, that is the identity under which you will actually be sending the Intent. The returned string is supplied by the system, so that an application can not spoof its package.
Returns
- The package name of the PendingIntent, or null if there is none associated with it.
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://www.cnblogs.com/captainbed
以上是关于PendingIntent详解的主要内容,如果未能解决你的问题,请参考以下文章
如何以挂起的意图启动 Activity 并清除所有后台堆栈?