2013-11-27 88 views
0

Intent和Pending Intent之間有什麼區別。基本上我知道Intent,它在startActivity(intent),startService(intent)調用時使用,也用在putExtra()中。可以使用Pending Intent。請澄清。解釋Intent和PendingIntent之間的區別

+0

看到這個:http://stackoverflow.com/a/4812421/2668136和這一個:http:// stackoverflow。com/a/15873786/2668136 – Fllo

+0

沒問題但你可以用例子來解釋,上面的鏈接關於意圖和意圖的意圖之間的權限差異說了請在使用許可的例子說明。 –

回答

0

Pending intent是意圖,將在稍後開始。
Normal intent被傳遞給startActivity(意向)或StartService(意向)時,開始的時候
一個的PendingIntent是你給到另一個應用程序令牌(例如通知,告警管理或其他第三方應用程序),它允許這個其他應用程序使用您的應用程序的權限來執行預定義的一段代碼。
要通過掛起的意圖執行廣播,請通過PendingIntent.getBroadcast()獲取PendingIntent。
要通過未決意圖執行活動,您需要通過PendingIntent.getActivity()接收活動。

1

A 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,看到這個答案: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

後兩者都非常簡單和偉大。
希望這是有益的,更容易理解。

相關問題