Android Program to Demonstrate Pending Intent

Here is source code of the Program demonstrate a Pending Intent in Android. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.

The PendingIntent class provides a mechanism for creating Intents that can be fired on your application’s behalf by another application at a later time. A Pending Intent is commonly used to package Intents that will be fired in response to a future event, such as a Widget or Notification being clicked.

Main Activity

 
package com.example.alert_broadcast_receiver;
 
import android.os.Bundle;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
public class MainActivity extends Activity {
 
      @Override
        protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.activity_main);
             Button b1 = (Button) findViewById(R.id.button1);
             b1.setOnClickListener(new View.OnClickListener() {
                 @Override
                  public void onClick(View v) {
                      // TODO Auto-generated method stub
                        startAlert(v);
                  }
		});
	}
 
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
                 getMenuInflater().inflate(R.menu.main, menu);
                 return true;
	}
 
	public void startAlert(View view) {
                EditText text = (EditText) findViewById(R.id.editText1);
                int time = Integer.parseInt(text.getText().toString());
                Intent intent = new Intent(this, MyReceiver.class);
                PendingIntent pend_intent = PendingIntent.getBroadcast(
                            this.getApplicationContext(), 0, intent, 0);
		//calls the alarm
                 AlarmManager alarmManager = (AlarmManager) getSystemService(
                                        ALARM_SERVICE);
                 alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
                   + (time * 10000), pend_intent);
	}
}

MyReceiver

 
package com.example.alert_broadcast_receiver;
 
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
 
public class MyReceiver extends BroadcastReceiver{
 
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
                  Toast.makeText(context, "Times Up!!!!", Toast.LENGTH_LONG).show();
	}
 
}

Activity_Main.xml

advertisement
 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="172dp"
        android:text="Start" />
 
    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/button1"
        android:layout_marginTop="63dp"
        android:ems="10" />
 
</RelativeLayout>

AndroidManifest.xml

Free 30-Day Java Certification Bootcamp is Live. Join Now!
 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.alert_broadcast_receiver"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.alert_broadcast_receiver.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
 
        <receiver android:name="MyReceiver" >
        </receiver>
    </application>
 
</manifest>

Alert_broadcast

Sanfoundry Global Education & Learning Series – 100+ Java Android Tutorials.

If you wish to look at all Tutorials, go to Java Android Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.