Change button background on click event , notification bar - android

i want to Change button background on click event , notification bar
for eg. i have button with text play which is on notification bar when i click on play it should be change to pause and again if i click on pause this pause text should be changed to play text, how can we do it any help is appreciated
thanks in advance
it is more like music player has on going notification so we can play and pause the media using notification bar as well.
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
RemoteViews contentv = new RemoteViews(getPackageName(),
R.layout.notification_bar);
int icon = R.drawable.noti_launcher;
CharSequence tickerText = "notification";
long when = System.currentTimeMillis(); // now
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
notification.contentView = contentv;
Intent laucherIntent = new Intent(this, NavigationActivity.class);
Intent i = new Intent(AlarmClock.ACTION_SET_ALARM);
PendingIntent launcherPI = PendingIntent.getActivity(this, 0,
laucherIntent, 0);
PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);
Intent flaotIntent = new Intent(this, FloatMainActivity.class);
PendingIntent floatPI = PendingIntent.getActivity(this, 0, flaotIntent,
0);
PendingIntent wifiPI = PendingIntent.getBroadcast(context, 0,
new Intent(Constants.ACTION_WIFI), 0);
PendingIntent btPI = PendingIntent.getBroadcast(context, 0, new Intent(
Constants.ACTION_BLUETOOTH), 0);
PendingIntent mobileDataPI = PendingIntent.getBroadcast(context, 0,
new Intent(Constants.ACTION_DATA), 0);
PendingIntent alarmsPI = PendingIntent.getBroadcast(context, 0,
new Intent(Constants.CUSTOM_ACTION), 0);
contentv.setImageViewResource(R.id.bluetooth, R.drawable.noti_wifi);
notification.flags |= Notification.FLAG_ONGOING_EVENT;
contentv.setOnClickPendingIntent(R.id.bluetooth, btPI);
contentv.setOnClickPendingIntent(R.id.wifi, wifiPI);
contentv.setOnClickPendingIntent(R.id.data, mobileDataPI);
contentv.setOnClickPendingIntent(R.id.alarm, pi);
contentv.setOnClickPendingIntent(R.id.icon, launcherPI);
contentv.setOnClickPendingIntent(R.id.float_notification, floatPI);
notificationManager.notify(1, notification);

contentv.setInt(R.id.viewId, "change", R.drawable.nextId);
you can use this for change background image.

Related

Open application after clicking on notification Android

I want to run my application after notification click!
I want to run: app-->com.zaglebiefm.Radio
This file diectory: library-->com.zaglebiefm.library.Notyfication
Notification.class:
private void buildNotification() {
Intent intentPlayPause = new Intent(NOTIFICATION_INTENT_PLAY_PAUSE);
Intent intentOpenPlayer = new Intent(NOTIFICATION_INTENT_OPEN_PLAYER);
Intent intentCancel = new Intent(NOTIFICATION_INTENT_CANCEL);
PendingIntent playPausePending = PendingIntent.getBroadcast(this, 23, intentPlayPause, 0);
PendingIntent openPending = PendingIntent.getBroadcast(this, 31, intentOpenPlayer, 0);
PendingIntent cancelPending = PendingIntent.getBroadcast(this, 12, intentCancel, 0);
RemoteViews mNotificationTemplate = new RemoteViews(this.getPackageName(), R.layout.notification);
Notification.Builder notificationBuilder = new Notification.Builder(this);
if (artImage == null)
artImage = BitmapFactory.decodeResource(getResources(), R.drawable.default_art);
mNotificationTemplate.setTextViewText(R.id.notification_line_one, singerName);
mNotificationTemplate.setTextViewText(R.id.notification_line_two, songName);
mNotificationTemplate.setImageViewResource(R.id.notification_play, isPlaying() ? R.drawable.btn_playback_pause : R.drawable.btn_playback_play);
mNotificationTemplate.setImageViewBitmap(R.id.notification_image, artImage);
mNotificationTemplate.setOnClickPendingIntent(R.id.notification_play, playPausePending);
Notification notification = notificationBuilder
.setSmallIcon(smallImage)
.setContentIntent(openPending)
.setPriority(Notification.PRIORITY_DEFAULT)
.setContent(mNotificationTemplate)
.setUsesChronometer(true)
.build();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
RemoteViews mExpandedView = new RemoteViews(this.getPackageName(), R.layout.notification_expanded);
mExpandedView.setTextViewText(R.id.notification_line_one, singerName);
mExpandedView.setTextViewText(R.id.notification_line_two, songName);
mExpandedView.setImageViewResource(R.id.notification_expanded_play, isPlaying() ? R.drawable.btn_playback_pause : R.drawable.btn_playback_play);
mExpandedView.setImageViewBitmap(R.id.notification_image, artImage);
mExpandedView.setOnClickPendingIntent(R.id.notification_expanded_play, playPausePending);
notification.bigContentView = mExpandedView;
}
if (mNotificationManager != null)
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
I tried some things but they won't work for me.
Any ideas or solutions.
Use the below code instead to launch desired/targetted application activity.
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context,YourTargetActivity.class), 0);
mBuilder.setContentIntent(pendingIntent);
This should work.
Intent myintent = new Intent(this, Splash_Activity.class);
myintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_NEW_TASK);
int randomPIN = (int)(Math.random()*9000)+1000;
PendingIntent conIntent = PendingIntent.getActivity(this, randomPIN,
myintent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(conIntent);
This will work

Notification with multiple buttons

I need to display notification with two buttons. Different operation need to perform for each button.so for that I have written following code but when I'm getting multiple notification delete action is not performing.
Random NOTIFICATION_ID = new Random();
int CANCELNOTIFICATIONID = NOTIFICATION_ID.nextInt();
// define sound URI, the sound to be played when there's a notification
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Log.i("******* Service6", "" + msg);
// intent triggered, you can add other intent for other actions
Intent intent = new Intent(GcmIntentService.this, LoginActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(GcmIntentService.this, 0, intent, 0);
Intent deleteIntent = new Intent(GcmIntentService.this, DeleteArchiveLoopActivity.class);
deleteIntent.putExtra(LoopMeConstants.EXTRA_DELETE_ARCHIVE_LOOPS, "Delete loops");
Trace.i(TAG, "Looptype Delete loop");
deleteIntent.putExtra("DELETE_ARCHIVE_LOOP_ID", loopId);
deleteIntent.putExtra("NOTIFICATONID", CANCELNOTIFICATIONID);
deleteIntent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
// PendingIntent pDeleteIntent = PendingIntent.getActivity(this, 145623, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent archiveIntent = new Intent(GcmIntentService.this, DeleteArchiveLoopActivity.class);
Trace.i(TAG, "Looptype Archive loop");
archiveIntent.putExtra(LoopMeConstants.EXTRA_DELETE_ARCHIVE_LOOPS, "Archive loops");
archiveIntent.putExtra("DELETE_ARCHIVE_LOOP_ID", loopId);
archiveIntent.putExtra("NOTIFICATONID", CANCELNOTIFICATIONID);
archiveIntent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
// PendingIntent pArchiveIntent = PendingIntent.getActivity(this, 145623, archiveIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setSmallIcon(R.drawable.ic_launcher);
notificationBuilder.setContentTitle("Sample");
notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(mPushtext));
notificationBuilder.setContentText(mPushtext);
notificationBuilder.setSound(soundUri);
notificationBuilder.addAction(R.drawable.delete, "Delete", PendingIntent.getActivity(this, 145623, deleteIntent, PendingIntent.FLAG_ONE_SHOT));
notificationBuilder.addAction(R.drawable.archive, "Archive", PendingIntent.getActivity(this, 145623, archiveIntent, PendingIntent.FLAG_UPDATE_CURRENT));
notificationBuilder.setAutoCancel(false);
// notificationBuilder.setContentIntent(pArchiveIntent);
Log.i("GCMIntent Srevice5", "" + msg);
notificationBuilder.setOngoing(true);
mNotificationManager.notify(CANCELNOTIFICATIONID, notificationBuilder.build());
How to solve this issue.
You need to use different requestCode in Pending intent
notificationBuilder.addAction(R.drawable.delete, "Delete", PendingIntent.getActivity(this, 1, deleteIntent, PendingIntent.FLAG_ONE_SHOT));
notificationBuilder.addAction(R.drawable.archive, "Archive", PendingIntent.getActivity(this, 2, archiveIntent, PendingIntent.FLAG_UPDATE_CURRENT));
In the below way i have solved it.
in the intent i have given two differentactivities
Intent deleteIntent = new Intent(GcmIntentService.this, DeleteLoopActivity.class);
Intent archiveIntent = new Intent(GcmIntentService.this, ArchiveLoopActivity.class);
notificationBuilder.addAction(R.drawable.delete, "Delete", PendingIntent.getActivity(this, CANCELNOTIFICATIONID, deleteIntent, 0));
notificationBuilder.addAction(R.drawable.archive, "Archive", PendingIntent.getActivity(this, CANCELNOTIFICATIONID, archiveIntent, 0));
The above code solves my problem
Thanks all

MediaPlayer Notification Controls Not Working

I have the following code that creates an ongoing notification for my music player
private Notification buildNotification(String title, String artist) {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext());
notificationBuilder.setOngoing(true);
notificationBuilder.setAutoCancel(false);
notificationBuilder.setSmallIcon(R.drawable.ic_stat_playing);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
new Intent(getApplicationContext(), MusicPlayer.class), PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pendingIntent);
RemoteViews compactNotification = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notfication_custom_layout);
RemoteViews expandedNotification = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_expanded_layout);
Intent previousTrackIntent = new Intent(Music.getContext(), MusicService.class);
previousTrackIntent.setAction(MusicService.ACTION_REWIND);
PendingIntent previousTrackPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, previousTrackIntent, 0);
Intent playPauseTrackIntent = new Intent(Music.getContext(), MusicService.class);
playPauseTrackIntent.setAction(MusicService.ACTION_TOGGLE_PLAYBACK);
PendingIntent playPauseTrackPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, playPauseTrackIntent, 0);
Intent nextTrackIntent = new Intent(Music.getContext(), MusicService.class);
nextTrackIntent.setAction(MusicService.ACTION_SKIP);
PendingIntent nextTrackPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, nextTrackIntent, 0);
Intent stopServiceIntent = new Intent(Music.getContext(), MusicService.class);
stopServiceIntent.setAction(MusicService.ACTION_STOP);
PendingIntent stopServicePendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, stopServiceIntent, 0);
if (mState == State.Playing) {
compactNotification.setImageViewResource(R.id.notification_base_play, R.drawable.ic_action_pause_dark);
expandedNotification.setImageViewResource(R.id.notification_expanded_base_play, R.drawable.ic_action_pause_dark);
} else if (mState == State.Paused || mState == State.Stopped) {
compactNotification.setImageViewResource(R.id.notification_base_play, R.drawable.ic_action_play_dark);
expandedNotification.setImageViewResource(R.id.notification_expanded_base_play, R.drawable.ic_action_play_dark);
}
compactNotification.setTextViewText(R.id.notification_base_line_one, title);
compactNotification.setTextViewText(R.id.notification_base_line_two, artist);
expandedNotification.setTextViewText(R.id.notification_base_line_one, title);
expandedNotification.setTextViewText(R.id.notification_base_line_two, artist);
compactNotification.setOnClickPendingIntent(R.id.notification_base_play, playPauseTrackPendingIntent);
expandedNotification.setOnClickPendingIntent(R.id.notification_base_play, playPauseTrackPendingIntent);
compactNotification.setOnClickPendingIntent(R.id.notification_base_next, nextTrackPendingIntent);
expandedNotification.setOnClickPendingIntent(R.id.notification_base_next, nextTrackPendingIntent);
compactNotification.setOnClickPendingIntent(R.id.notification_base_previous, previousTrackPendingIntent);
expandedNotification.setOnClickPendingIntent(R.id.notification_base_previous, previousTrackPendingIntent);
expandedNotification.setOnClickPendingIntent(R.id.notification_expanded_base_collapse, stopServicePendingIntent);
compactNotification.setOnClickPendingIntent(R.id.notification_base_collapse, stopServicePendingIntent);
//Set the album art.
expandedNotification.setImageViewBitmap(R.id.notification_expanded_base_image, BitmapFactory.decodeResource(getResources(), R.drawable.selena_gomez));
compactNotification.setImageViewBitmap(R.id.notification_base_image, BitmapFactory.decodeResource(getResources(), R.drawable.selena_gomez));
//Attach the shrunken layout to the notification.
notificationBuilder.setContent(compactNotification);
//Build the notification object.
Notification notification = notificationBuilder.build();
//Attach the expanded layout to the notification and set its flags.
if (Build.VERSION.SDK_INT >= 16)
notification.bigContentView = expandedNotification;
notification.flags = Notification.FLAG_FOREGROUND_SERVICE |
Notification.FLAG_NO_CLEAR |
Notification.FLAG_ONGOING_EVENT;
return notification;
}
The notification appears correctly but the controls dont work even thought the controls are well defined.
Is there something else I need to enable or assign to get the controls to work?
I know this answer is 5 years late, but I found that Notification.FLAG_FOREGROUND_SERVICE still shows the controls, but doesn't allow them to be "clickable". I found the same behavior when I showed the notification with startForeground(int,notification);
If I just used the following flags (Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT), then call stopForeground(false); and then nm.notify(int, notification); it works as expected.

Android custom notification bar

I do own according to the notification bar: http://developer.android.com/guide/topics/ui/notifiers/notifications.html
I do like the tutorial :
Notification notification = new Notification();
notification.flags = Notification.FLAG_ONGOING_EVENT;
notification.icon = icon;
RemoteViews contentView = new RemoteViews(getPackageName(),
R.layout.custom_notification);
contentView.setImageViewResource(R.id.image, R.drawable.b_10);
contentView.setTextViewText(R.id.title, "Custom zgłoszenie");
contentView.setTextViewText(R.id.text, "test test test");
notification.contentView = contentView;
NotificationIntent Intent = new Intent(BatteryInfoService.this,
BatteryInfoActivity.class);
ContentIntent PendingIntent = PendingIntent.getActivity(ta, 0,
notificationIntent, 0);
notification.contentIntent = contentIntent;
mNotificationManager.notify(BATTERY_ID, notification);
There are errors in the lines:
NotificationIntent Intent = new Intent(BatteryInfoService.this,
BatteryInfoActivity.class);
ContentIntent PendingIntent = PendingIntent.getActivity(ta, 0,
notificationIntent, 0);
notification.contentIntent = contentIntent;
Errors:
NotificationIntent cannot be resolved to a type
Multiple markers at this line
- ContentIntent cannot be resolved to
a type
- ta cannot be resolved to a variable
contentIntent cannot be resolved to a variable
Replace NotificationIntent with Intent (android does not provide NotificationIntent, unless its a custom class).
what you are looking for is
Intent notificationIntent = new Intent(BatteryInfoService.this,BatteryInfoActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Please read the tutorial carefully. Intent and PendingIntent are android Classes where as NotificationIntent and ContentIntent are not. If you have made custom classes under those names and you can import the appropriate packages.

Why my notification doesn't appear

Hello guys Im using advance notification manager this is the code
Notification note = new Notification();
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notif);
contentView.setImageViewResource(R.id.image, R.drawable.notif);
contentView.setTextViewText(R.id.title, "Focused Crawling");
contentView.setTextViewText(R.id.text, "Crawling In Progress....");
note.contentView = contentView;
Intent notificationIntent = new Intent(this, LoadingActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
note.contentIntent = contentIntent;
note.vibrate = new long[] { 500L, 200L, 200L, 500L };
note.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "2");
note.flags |= Notification.FLAG_NO_CLEAR;
mgr.notify(1337, note);
startForeground(1337, note);
and why it didnt appear at the status bar ?
You don't specify an icon for the status bar, which is required.
Add a line like this to your code:
note.icon = R.drawable.youricon;
See Creating a Notification

Categories

Resources