Differences between Intent and PendingIntent

I read some articles, and both seem to be doing the same thing, and I was wondering what is the difference between starting such a service:

Intent intent = new Intent(this, HelloService.class); startService(intent); 

or so:

 Calendar cal = Calendar.getInstance(); Intent intent = new Intent(this, MyService.class); PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0); AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE); alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30*1000, pintent); 

As I read, these two do the same if you return the parameter START_STICKY in the service;

+80
android android-service
Jun 17 '14 at 6:42
source share
5 answers

Intention

Android Intent is an object that carries an intent, that is, a message from one component to another component inside or outside the application. Skills can transfer messages between any three main components of the application - "Actions, services" and "BroadcastReceivers".

Intent itself, an Intent, is a passive data structure. It contains an abstract description of the operation being performed.

For example: let's say you have an Activity that an email client should launch and send an email. To do this, your activity will send an Intent with the ACTION_SEND action along with the corresponding choice in the Android Intent Resolver:

 Intent intent = new Intent(Intent.ACTION_SENDTO); intent.setData(Uri.parse("mailto:")); // only email apps should handle this 

This selection gives you the right interface for the user to choose how to send your email data.

EXPLICIT INTENTS

 // Explicit Intent by specifying its class name Intent i = new Intent(this, TargetActivity.class); i.putExtra("Key1", "ABC"); i.putExtra("Key2", "123"); // Starts TargetActivity startActivity(i); 

INFLUENCE OF THE STATEMENT

 // Implicit Intent by specifying a URI Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com")); // Starts Implicit Activity startActivity(i); 

Awaiting Intent

A PendingIntent is a token that you provide to an external application (for example, NotificationManager, AlarmManager, Home Screen AppWidgetManager or other third-party applications), which allows a foreign application to use the permissions of your application to execute a predefined piece of code.

By giving PendingIntent to another application, you grant it the right to perform the specified operation as if the other application was itself (with the same rights and identification). In the form of, for example, you have to be careful about how you create the PendingIntent: almost always, for example, the underlying intent that you supply should have a component name explicitly set to one of your own components, to ensure it is ultimately sent there and nowhere else.

Example of Expected Intent: http://android-pending-intent.blogspot.in/

Source: Android Intent and Expected Android Intent

Hope this helps.

+129
Jun 17 '14 at 10:53 on
source share

PendingIntent is an Intent wrapper. The foreign application that receives the PendingIntent does not know the contents of the Intent that is wrapped by the PendingIntent . The mission of a foreign application is to send its intention to the owner when certain conditions are met (for example: an alarm clock with a schedule or a notification with a click ...). Conditions are issued by the owner, but are processed by a foreign application (for example: alarm clock, notification).

If a foreign application sends an intention to your application, it means that the foreign application is aware of the contents of the intention. and the foreign application decides to send the intention, then your application must process the intention to fulfill some conditions => your application will receive a system performance resource.

+21
Jan 25 '16 at 14:01
source share

Another simple difference:

  • Normal intention will die as soon as the application is killed.

  • Pending intentions never die. They will be alive as long as it is necessary for the alert service, location service or any other services.

+2
Oct 30 '17 at 2:50
source share

Starting Services Regularly Using AlarmManager

As with actions, Android can terminate the service process at any time to save resources. For this reason, you cannot just use TimerTask in a service to make sure that it runs on a regular basis.

So, to plan the service properly, use the AlarmManager class.

UPDATE:

Thus, there is no real difference between the two. But depending on whether you want to ensure the execution of the service or not, you can decide what to use, since for the first there is no guarantee, but for a later version.

More information on AndroidServices .

+1
Jun 17 '14 at 6:52
source share

Functionally, there is no difference.

The value of PendingIntent is that you can process it in another application, which can subsequently use it as if it were another application. Here is the relevant explanation from the documentation :

By giving PendingIntent to another application, you grant it the right to perform the specified operation as if the other application was itself (with the same rights and identification). In the form of, for example, you have to be careful about how you create the PendingIntent: almost always, for example, the underlying intent that you supply should have a component name explicitly set to one of your own components, to ensure it is ultimately sent there and nowhere else.

A PendingIntent itself is simply a reference to a token, a supported system that describes the source data used to extract it.

So PendingIntent is just a reference to the data that represents the original intent (which is used to create the PendingIntent).

+1
Jun 17 '14 at 10:25
source share



All Articles