Thursday, 10 September 2015

Expandable custom notification Handle event by using BroadcastReceiver

I am writing code for handling action event from notification by using BroadcastReceiver and also you can handle by using other way with in same activity.

MainActivity.java

package com.example.addactionnotification;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends ActionBarActivity implements OnClickListener {
 static String CLOSE_ACTION = "close_action";
 static String ACCEPT_ACTION = "accept_action";
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 }

 @Override
 public void onClick(View view) {

  switch (view.getId()) {
  case R.id.btnAdddActionNotification:

   showAddActionNotification();
   break;
  default:
   break;
  }
 }

 private void showAddActionNotification() {

  int icon = R.drawable.ic_launcher;

  NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

  builder.setSmallIcon(icon);

  builder.setContentText("Add action notification").setContentTitle("Here is accept and close action only");

  Intent closeIntent = new Intent(this, MyReceiver.class);


  closeIntent.setAction(CLOSE_ACTION);


  PendingIntent closePendingIntent = PendingIntent.getBroadcast(this, 12345, closeIntent, PendingIntent.FLAG_UPDATE_CURRENT);

  builder.addAction(R.drawable.history, "Close", closePendingIntent);

  Intent acceptIntent = new Intent();
  acceptIntent.setAction(ACCEPT_ACTION);

  PendingIntent acceptPendingIINtent = PendingIntent.getBroadcast(this, 12345, acceptIntent, PendingIntent.FLAG_UPDATE_CURRENT);


  builder.addAction(R.drawable.contact, "Accept", acceptPendingIINtent);

  Notification notification = builder.build();


  Intent notificationIntent = new Intent(this, MainActivity.class);

  PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);


  notification.contentIntent = contentIntent;

  notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_SHOW_LIGHTS;
  NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

  mNotificationManager.notify(1, notification);
 }
}

MyReceiver.java
package com.example.addactionnotification;
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) {
  String action = intent.getAction();
  if (MainActivity.CLOSE_ACTION.equals(action)) {

   // write code for close action
   Toast.makeText(context, "Hello close action", Toast.LENGTH_LONG).show();

  } else if (MainActivity.ACCEPT_ACTION.equals(action)) {
   Toast.makeText(context, "Hello close action", Toast.LENGTH_LONG).show();
   // write code for accept action
  }
 }
}

activity_main.xml
<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.addactionnotification.MainActivity" >

    <Button       android:id="@+id/btnAdddActionNotification"
      android:layout_width="wrap_content"     android:layout_centerHorizontal="true"      android:layout_centerVertical="true"
        android:onClick="onClick"      android:text="@string/addActionNotification"
    android:layout_height="wrap_content" />
</RelativeLayout>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.addactionnotification"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="21" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:excludeFromRecents="true"
            android:label="@string/app_name"
            android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="com.example.addactionnotification.MyReceiver">
        </receiver>
    </application>
</manifest>

No comments:

Post a Comment