Monday, 8 February 2016

PendingIntent in android


A PendingIntent is a token that you give to a foreign application (e.g. NotificationManager , AlarmManager , Home Screen AppWidgetManager , or other 3rd party applications), which allows the foreign application to use your application's permissions to execute a predefined piece of code.


PendingIntents can be created of three typesgetService(Context, int, Intent, int);


Here is my sample code for launch applications activity with pendingIntent.

here need to create two activity,

one for setup pendingIntent and another activity to run after the pendingintent fired.

MainActivity.Java

Setup pendingintent with the notification.Once the notification selected it will lunch the Notification activity.

 package com.example.velmurugan.pendingintentexample;  
 import android.app.Notification;  
 import android.app.NotificationManager;  
 import android.app.PendingIntent;  
 import android.content.Intent;  
 import android.support.v4.app.NotificationCompat;  
 import android.support.v7.app.AppCompatActivity;  
 import android.os.Bundle;  
 import android.view.View;  
 import android.widget.Button;  
 public class MainActivity extends AppCompatActivity {  
   private Button button;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     button = (Button)findViewById(R.id.button);  
   }  
   public void showNotification(View view){  
     Intent activityIntent = new Intent(this,NotificationActivity.class);  
     activityIntent.putExtra("intentData","Your data");  
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);  
     NotificationCompat.Builder builder = new NotificationCompat.Builder(this);  
     builder.setContentTitle("Title");  
     builder.setContentText("Content");  
     builder.setSmallIcon(R.drawable.ic_launcher);  
     builder.setContentIntent(pendingIntent);  
     builder.setAutoCancel(true);  
     Notification notification = builder.build();  
     NotificationManager notificationManager =(NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);  
     notificationManager.notify(0,notification);  
   }  
 }  


activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <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="com.example.velmurugan.pendingintentexample.MainActivity">  
   <Button  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Show notification"  
     android:id="@+id/button"  
     android:layout_centerVertical="true"  
     android:layout_centerHorizontal="true"  
     android:onClick="showNotification"/>  
 </RelativeLayout>  


NotificationActivity.Java

 package com.example.velmurugan.pendingintentexample;  
 import android.app.Activity;  
 import android.content.Intent;  
 import android.os.Bundle;  
 import android.widget.TextView;  
 /**  
  * Created by velmurugan on 7/2/16.  
  */  
 public class NotificationActivity extends Activity {  
   private TextView textView;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.notification_layout);  
     textView = (TextView)findViewById(R.id.textView);  
     Intent intent = this.getIntent();  
     String intentData = intent.getStringExtra("intentData");  
     textView.setText(intentData);  
   }  
 }  


notification_layout.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:orientation="vertical" android:layout_width="match_parent"  
   android:layout_height="match_parent">  
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:textAppearance="?android:attr/textAppearanceLarge"  
     android:id="@+id/textView" />  
 </LinearLayout>  


No comments: