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

}


22 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. It is really a great work and the way in which u r sharing the knowledge is excellent.Thanks for helping me to understand basic concepts. As a beginner in android programming your post help me a lot.Thanks for your informative article. Android Training in velachery | Android Training institute in chennai

    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 information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.Android Training in chennai with placement | Android Training in velachery

    ReplyDelete