解释Intent和PendingIntent之间的区别

时间:2013-11-27 02:15:03

标签: android

Intent和Pending Intent之间有什么区别。基本上我知道Intent,它在startActivity(intent),startService(intent)调用时使用,也在putExtra()中使用。但是我可以使用Pending Intent.Please澄清。

2 个答案:

答案 0 :(得分:1)

PendingIntentActivityBroadcastService

为什么要使用PendingIntent
例如,如果你想创建一个Bluetooth Intent,你可以这样做:

Intent myIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(myIntent);  

您无权启用Bluetooth android.permission.BLUETOOTH_ADMIN,因此您必须创建PendingIntent并使用它,您无需拥有此权限新Intent
换句话说:这允许外部应用程序使用您的应用程序的权限来执行预定义的代码段。

这是您给另一个应用程序(例如NotificationManager,AlarmManager或第三个应用程序)的令牌。

您可以开始新的活动getActivity(Context context, int requestCode, Intent intent, int flags)
执行广播getBroadcast(Context context, int requestCode, Intent intent, int flags)
或者开始服务getService(Context context, int requestCode, Intent intent, int flags)

"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."  

您可以使用PendingIntent创建新的flags或使用现有的{{1}},请参阅以下答案:https://stackoverflow.com/a/15298131/2668136


有关更多信息,请参阅这些教程:
1. Simple Launch Activity with PendingIntent
2. Alarm Service using AlarmManager
3. Android Notifications using NotificationManager
4. Using AlarmManager and BroadcastReceiver

后两者非常简单而且很棒 希望这有用且更易于理解。

答案 1 :(得分:0)

Pending intent是意图,将在稍后开始。
Normal intent从传递给startActivity(intent)或StartService(intent)时开始
PendingIntent是您为另一个应用程序(例如,通知管理器,警报管理器或其他第三方应用程序)提供的令牌,它允许此其他应用程序使用您的应用程序的权限来执行预定义的代码段。
通过挂起的意图执行广播,因此通过PendingIntent.getBroadcast()获取PendingIntent。
要通过挂起的意图执行活动,您将通过PendingIntent.getActivity()接收活动。