Monday 21 September 2015

NotificationPanel with Button

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.RemoteViews;

public class NotificationPanel {

private Context parent;
private NotificationManager nManager;
private NotificationCompat.Builder nBuilder;
private RemoteViews remoteView;

public NotificationPanel(Context parent) {
// TODO Auto-generated constructor stub
this.parent = parent;
nBuilder = new NotificationCompat.Builder(parent)
.setContentTitle("")
.setSmallIcon(R.drawable.ic_launcher).setOngoing(true);

remoteView = new RemoteViews(parent.getPackageName(),
R.layout.notificationview);

// set the button listeners
setListeners(remoteView);
nBuilder.setContent(remoteView);

nManager = (NotificationManager) parent
.getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(2, nBuilder.build());
}

public void setListeners(RemoteViews view) {
// listener 1
Intent volume = new Intent(parent, CameraONOFFService.class);



PreferenceSetting preferenceSetting = PreferenceSetting
.getInstance(parent);

if (!preferenceSetting.getON())
{

volume.putExtra("DO", "on");
view.setImageViewResource(R.id.btn1, R.drawable.off);
view.setInt(R.id.lay_main, "setBackgroundColor", android.graphics.Color.WHITE);
}
else
{

volume.putExtra("DO", "off");

view.setImageViewResource(R.id.btn1, R.drawable.on);
view.setInt(R.id.lay_main, "setBackgroundColor", android.graphics.Color.BLACK);
}

PendingIntent btn1 = PendingIntent.getService(parent, 0, volume, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.lay_main, btn1);

}

public void notificationCancel() {
nManager.cancel(2);
}
}

No comments:

Post a Comment