An application can create a Pending Intent, which is nothing but an intent and action to it, so that it can passed to some other applications which can execute this pendingintent as if original application executes it.
PendingIntents can be created of three types
getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int),
getService(Context, int, Intent, int);
You can create a pending intent to launch an activity, or service, broadcast an intent.
Let us see how to create an pendingIntent which can launch an activity and see once you have pendingIntent, how can you launch make operation on the intent.
- Create a two activities in your android project.
- One activity create an pendingIntent through 'getActivity()' api
PendingIntent pendingIntent;
Intent intent = new Intent();
intent.setClass(mContext,activity2.class);
pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
- pass activity2 as setClass parameter to intent and create the pendingIntent.
- In activty1, for button click action, use the pendingIntent instance to perform the operation on it using 'send()' api.
Intent intent = new Intent();
try {
pendingIntent.send(mContext, 0, intent);
} catch (PendingIntent.CanceledException e) {
// the stack trace isn't very helpful here. Just log the exception message.
System.out.println( "Sending contentIntent failed: " );
}
activity1.java
---------------
package com.mani.pending;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class activity extends Activity {
Button b1;
PendingIntent pendingIntent;
Context mContext;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContext = this.getApplicationContext();
b1 = (Button) findViewById(R.id.button);
Intent intent = new Intent();
intent.setClass(mContext,activity2.class);
pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
b1.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
try {
pendingIntent.send(mContext, 0, intent);
} catch (PendingIntent.CanceledException e) {
// the stack trace isn't very helpful here. Just log the exception message.
System.out.println( "Sending contentIntent failed: " );
}
}
});
}
}
activity2.java
----------------
package com.mani.pending;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class activity2 extends Activity{
TextView v1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
v1 = (TextView)findViewById(R.id.text);
v1.setTextSize(30);
v1.setText("Welcome to pendinIntent");
}
}
Thanks for this post. It was helpful to me.
ReplyDelete------
Swathi
Can you please tell me how does your example differ from an intent. This example given above can be very well implemented in a much easier form with an intent
ReplyDeleteplz check here http://android-pending-intent.blogspot.in/
Deletethink , you have two application , A(notification) and B facebook whenever you press in notification your control is redirected to Facebook . but now you will say that it is same as intent .
Deletenow think about Alarm , that at 4 am please automatically open my all received notification in a facebook app .
See this line :
DeletependingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
here "getApplicationContext()" context is The Context in which this PendingIntent should start the activity (Facebook).
Now see this line :
pendingIntent.send(getApplicationContext(), 0, intent);
here getApplicationContext(), The Context of the caller( notification ) , and for 4 pm you can start your service and at the particular time your pending Intent will play its game .
nice done....
ReplyDeleteThanks for this post...!
ReplyDeletethank you for the post
ReplyDeletewhy not to write shorter?
ReplyDeletefor example, instead of this:
PendingIntent pendingIntent;
Intent intent = new Intent();
intent.setClass(mContext,activity2.class);
pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
write just:
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext,activity2.class), 0);
Regards.
Itz just a startup tutorial for newbees dude...Very helpful indeed. why dont u publish ur own blog and type code in such format..
ReplyDeleteHello buddy,
ReplyDeleteCould you please tell me how to start the service from activity with pending intent and in service class i need to do some scheduled task repaetdly.and update the GUI regularly.
Please suggest some thing.
Regards.
HI Aditya,
ReplyDeleteLike these we update our Ui every 30 minutes.here workIntent is parentIntent
PendingIntent pendingIntent = PendingIntent.getService(this, 0, workIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
long trigger = System.currentTimeMillis() + 30 * 60000;
alarmManager.set(AlarmManager.RTC_WAKEUP, trigger, pendingIntent);
can u give me one real time example for this Pending Intent ? .which is easy to understand pending intent.
ReplyDeleteheyy, Thanks for your post...do u have any idea how to get event for Launching of Pending Intent...like how we get onClick..onTouch...any idea?
ReplyDeleteNice post. Can u plz give another example of Pending intent using broadcast?
ReplyDeletehow baked explanation ...
ReplyDeletePerfect explanation about launch pending intent..Android Training
ReplyDeleteThanks for this brief example!
ReplyDeleteGiven so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more .
ReplyDeleteAndroid Training in chennai | Android Training in chennai with placement
Thanks for sharing such an informative blog.Awaiting for your next update.
ReplyDeleteSelenium Training in chennai | Selenium Training in annanagar | Selenium Training in omr | Selenium Training in porur | Selenium Training in tambaram | Selenium Training in velachery
After reading your article I was amazed. I know that you explain it very well. And I hope that other readers will also experience how I feel after reading your article.
ReplyDeleteiot certification Online Training in bangalore
iot certification courses in bangalore
iot certification classes in bangalore
iot certification Online Training institute in bangalore
iot certification course syllabus
best iot certification Online Training
iot certification Online Training centers
Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot.
ReplyDeleteHadoop Admin Online Training
Hadoop Admin Classes Online
Hadoop Admin Training Online
Online Hadoop Admin Course
Hadoop Admin Course Online
This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me.
ReplyDeleteCognos Online Training
Cognos Classes Online
Cognos Training Online
Online Cognos Course
Cognos Course Online
android development course
ReplyDeleteandroid training
Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.
ReplyDeleteAWS Training in Pune
Best RPA Training in Pune
Hızlı takipçi almak için takipçi satın al
ReplyDeleteOrganik takipçi almak için takipçi satın al
Bilgisayardan takipçi almak için takipçi satın al
Mobil cihazdan takipçi almak için takipçi satın al
Gerçek ve orijinal takipçi almak için takipçi satın al
Yazarkasa ile takipçi almak için takipçi satın al
Bitcoin takipçi almak için takipçi satın al
Pos ile takipçi almak için takipçi satın al
EFT ile takipçi almak için takipçi satın al
Havale ile takipçi almak için takipçi satın al
Mobil ödeme ile takipçi almak için takipçi satın al
Tamamı orijinal takipçi almak için takipçi satın al
Organik ile takipçi almak için takipçi satın al
Türkiye takipçi almak için takipçi satın al
Global takipçi almak için takipçi satın al
En hızlı instagram takipçi satın al
En uygun instagram takipçi satın al
En telafili instagram takipçi satın al
En gerçek spotify takipçi satın al
En ucuz instagram takipçi satın al
En otomatik instagram takipçi satın al
En sistematik tiktok takipçi satın al
En otantik instagram takipçi satın al
En opsiyonel instagram takipçi satın al
En güçlü instagram takipçi satın al
En kuvvetli instagram takipçi satın al
En seri instagram takipçi satın al
En akıcı instagram takipçi satın al
En akıcı instagram takipçi satın al
En akıcı instagram takip etmeyenler
marsbahis
ReplyDeletetrendbet
galabet
perde modelleri
ReplyDeleteMobil Onay
mobil ödeme bozdurma
nft nasıl alınır
Ankara Evden Eve Nakliyat
trafik sigortası
dedektör
Web Sitesi Kurmak
Aşk Kitapları
smm panel
ReplyDeleteSmm panel
İş ilanları
instagram takipçi satın al
hirdavatciburada.com
beyazesyateknikservisi.com.tr
Servis
Tiktok hile
ReplyDeleteUseful Information.Excellent article. Keep writing such information on your site. You can come to India but you will need a visa. You can apply for an online visa for India. You can read all the info about Indian visas via our website.
Thank you for your treasured and beneficial information via the weblog. I am appreciating the way you shared the applicable, treasured, and perfect facts. We provide database services, If you have any query related to database services then visit our website will get complete information related to your query.
ReplyDeleteI in looking for of reality appreciated perusing your weblog. It transformed into every one in all adroitly wrote and simple to undertand. rather than auxiliary sites I have legitimate to utilize that are in mean of reality now not tht top notch. I after that decided your posts really alluring. As a matter of fact in the wake of examining, I expected to move ham it occurring it to my buddy and he ejoyed it as quickly! Windows 7 Ultimate Product Key
ReplyDeleteWow! Such a very informative article... Thanks for sharing this information...please continue... The travelers who want to travel India for vacation, sightseeing, and purpose to enjoy need a tourist visa India. If you want to know more about it, just click and check.
ReplyDeleteGood content. You write beautiful things.
ReplyDeletesportsbet
hacklink
mrbahis
sportsbet
vbet
taksi
vbet
korsan taksi
mrbahis
salt likit
ReplyDeletesalt likit
dr mood likit
big boss likit
dl likit
dark likit
1K4OX1
دهانات زيتية
ReplyDeleteدهانات اخشاب
Wonderful work! Your blog offers a new and insightful perspective on the subject. I appreciate how you simplify complicated concepts for a broad audience. Your writing style keeps the reader engaged from start to finish. The visuals enhance the text, providing a full and satisfying experience. Thanks for sharing your knowledge. Can't wait for more content like this! Keep it going!
ReplyDeleteBrilliant article! Your skill in simplifying intricate topics into manageable portions is commendable. The integration of practical instances adds depth and resonance to your message, making it all the more impactful. It's clear you've put a lot of thought and effort into crafting this piece, and the result is truly enlightening. Excited to see what you'll share next! Keep up the excellent work! 🌟📖 #InspiringContent #ThoughtfulWriting.
ReplyDelete