Android: How To Create A Custom Notification And Edit Its Layout Adding Views Programmatically?
Solution 1:
Notification area uses RemoteViews
just like Android App Widgets.
They are not like the UI parts in your application. You have reference to them, you can create them but they reside in other apps. (Notifications' UI is in System app, and app widgets is in Home Launcher app.)
Since they are not really shown in your app, you cannot directly access them. You cannot set onClickListener
for example.
That's why we have RemoteViews
.
With them, you can define how to create them by giving the layout file and some built-in functions to change the text, image etc. And you give PendingIntent
to be fired when a button is clicked. That's it.
In the end, you can actually change the UI in your notification dynamically but not the way you usually do in your app.
You can see this answer for how to create one. https://stackoverflow.com/a/21283668/1016462
Solution 2:
With notification style you can change it just with recreating a new notify with new style .It cost small blink (like refresh). But works.
Make your objects public static for first step .
Example :
publicstatic NotificationCompat.Builder mBuilder;
publicstatic RemoteViews contentBigCustom;
publicstatic RemoteViews contentSmallNotExpand;
publicstatic RemoteViews contentBigCustomFROMCODE;
Next - make this line :
notification.contentView = contentBigCustom;
to looks like :
// in top publicstatic RemoteViews contentAny;
Maybe you are using onStartCommand to initial objects thas good...
if (IWANTNOTIFYSTYLE == 0) {
contentBigCustom = RemoteViews(this.getPackageName(),R.layout.notificationBig);
}
else {
contentBigCustomFROMCODE = RemoteViews(this.getPackageName(),R.layout.notificationBig);
}
contentSmallNotExpand = RemoteViews(this.getPackageName(),R.layout.notificationSmall);
contentBigCustomFROMCODE = RemoteViews(this.getPackageName(),R.layout.notificationBig);
mBuilder = new NotificationCompat.Builder(this);
mBuilder.setDefaults(Notification.DEFAULT_ALL);
mBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
mBuilder.setSmallIcon(R.drawable.play);
mBuilder.setContentText("This text is not in function but its needed to create notify");
mBuilder.setLargeIcon(icon);
mBuilder.setPriority(Notification.PRIORITY_MAX);
mBuilder.setContent(contentSmallNotExpand);
mBuilder.setCustomBigContentView(contentBigCustom);
After notify exe line :
mNotificationManager.notify(0, mBuilder.build());
put :
stopSelf()
Too kill services . When you want to change style start again services and put action id for style 1 2 .. n . It look like refresh because we recreate new notify bar. This is only way 100% for now! If somebody think diferent please post me link to the app example.
You can make Activity object also public static .
Its is very easy now to control any combination :
Service - Activity Service - BroadcastReceiver BroadcastReceiver - Activity
But! :
Service1.contentNotify.setImageViewResource(R.id.NOTYFY_BTN, R.drawable.stop);
...
setImageViewResource Cant use this line for notification or any other style changes methods.
Only way is to create service , put on startCommand CREATE_NOTIFY() Find from intent action name what notify.xml you want to load . After notify build and add close service , then from broadcast call :
IntentserviceIntent=newIntent("WITHSTOP");
serviceIntent.putExtra("WITHSTOP", "1");
// must be MyApp extend Application reason : from broadcaster you cant access getContext and startService etc.
MyApp.getInstance().startService(serviceIntent);
Looks like :
publicclassMyAppextendsApplication {
privatestaticMyApp instance;
publicstaticMyAppgetInstance() {
return instance;
}
publicstaticContextgetContext(){
return instance;
// or return instance.getApplicationContext();
}
@OverridepublicvoidonCreate() {
instance = this;
super.onCreate();
}
}
...
Post a Comment for "Android: How To Create A Custom Notification And Edit Its Layout Adding Views Programmatically?"