Monday, March 28, 2011

how to launch pending intent

Pending Intent:

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");
}

}


36 comments:

  1. Thanks for this post. It was helpful to me.

    ------
    Swathi

    ReplyDelete
  2. 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

    ReplyDelete
    Replies
    1. plz check here http://android-pending-intent.blogspot.in/

      Delete
    2. think , 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 .

      now think about Alarm , that at 4 am please automatically open my all received notification in a facebook app .

      Delete
    3. See this line :

      pendingIntent = 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 .

      Delete
  3. Thanks for this post...!

    ReplyDelete
  4. thank you for the post

    ReplyDelete
  5. why not to write shorter?
    for 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.

    ReplyDelete
  6. Itz just a startup tutorial for newbees dude...Very helpful indeed. why dont u publish ur own blog and type code in such format..

    ReplyDelete
  7. Hello buddy,
    Could 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.

    ReplyDelete
  8. HI Aditya,
    Like 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);

    ReplyDelete
  9. can u give me one real time example for this Pending Intent ? .which is easy to understand pending intent.

    ReplyDelete
  10. heyy, 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?

    ReplyDelete
  11. Nice post. Can u plz give another example of Pending intent using broadcast?

    ReplyDelete
  12. how baked explanation ...

    ReplyDelete
  13. Perfect explanation about launch pending intent..Android Training

    ReplyDelete
  14. Thanks for this brief example!

    ReplyDelete
  15. Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more .
    Android Training in chennai | Android Training in chennai with placement

    ReplyDelete
  16. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me.

    Cognos Online Training

    Cognos Classes Online

    Cognos Training Online

    Online Cognos Course

    Cognos Course Online

    ReplyDelete
  17. 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.
    AWS Training in Pune
    Best RPA Training in Pune

    ReplyDelete
  18. Hızlı takipçi almak için takipçi satın al
    Organik 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

    ReplyDelete

  19. Useful 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.

    ReplyDelete
  20. 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.

    ReplyDelete
  21. I 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

    ReplyDelete
  22. Wow! 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.

    ReplyDelete