Android: How to create a custom notification and edit its layout adding views programmatically?

假装没事ソ 提交于 2020-01-04 06:46:22

问题


I'd like to know how to create a custom notification and edit is layout adding views programmatically to allow user to customize is notification.

What I can't do is this:

If I create a custom LinearLayout like this:

LinearLayout ll = new LinearLayout(c);
ImageView iv = new ImageView(c);
iv.setImageResource(R.drawable.bt_close);
ll.addView(iv);

How I can create a notification using this layout and add an action to do when I click on ImageView?

The code to make a custom notification is only this?

Notification notification = (statusIcon, appName, System.currentTimeMillis());
notification.contentView = new RemoteViews(this.getPackageName(),R.layout.notification);

Many thanks at all...


回答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




回答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 :

public static NotificationCompat.Builder mBuilder;
public static RemoteViews contentBigCustom;
public static RemoteViews contentSmallNotExpand;
public static RemoteViews contentBigCustomFROMCODE;

Next - make this line :

notification.contentView = contentBigCustom;

to looks like :

 // in top 
 public static 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 :

  Intent serviceIntent = new Intent("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 :

public class MyApp extends Application {
    private static MyApp instance;

    public static MyApp getInstance() {
        return instance;
    }

    public static Context getContext(){
        return instance;
        // or return instance.getApplicationContext();
    }

    @Override
    public void onCreate() {
        instance = this;
        super.onCreate();
    }
}

...



来源:https://stackoverflow.com/questions/11557353/android-how-to-create-a-custom-notification-and-edit-its-layout-adding-views-pr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!