自定义notification布局,加仿消息通知悬浮效果,加通知中控件点击事件 刷新界面的代码实现。

第一部分java文件:
package com.lenovo.dinghao1.mydialog.notification;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews;

import com.lenovo.dinghao1.mydialog.R;

public class MyNotification {
    private Context mContext;
    private NotificationManager notificationManager =null;
    private Notification mNotification = null;

    public MyNotification(Context context){
        mContext = context;
        init();
    }

    private void init(){
        IntentFilter intent = new IntentFilter();
        intent.addAction("android.intent.action_MTP");
        intent.addAction("android.intent.action_PTP");
        intent.addAction("android.intent.action_CHAR");
        mContext.registerReceiver(new MyNotificationReceiver(),intent);

    }

    public void createCustomNotification(int code,String channel_id,String channel_name){
        notificationManager = (NotificationManager) mContext.getApplicationContext().getSystemService(mContext.NOTIFICATION_SERVICE);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            NotificationChannel channel = new NotificationChannel(channel_id,channel_name, NotificationManager.IMPORTANCE_HIGH);
            channel.setLightColor(R.color.account_item_add);
            channel.canShowBadge();
            channel.enableVibration(false);
            channel.getGroup();
            channel.shouldShowLights();
            notificationManager.createNotificationChannel(channel);
        }
        Notification.Builder builder = getBuilder(channel_id,"title","this is noti!");
        builder.setDefaults(NotificationCompat.FLAG_ONLY_ALERT_ONCE);
        RemoteViews remoteViews = null;

        remoteViews = new RemoteViews(mContext.getApplicationContext().getPackageName(),R.layout.notifaction_layout);
        if(remoteViews==null){
            return;
        }
        builder.setCustomContentView(remoteViews);

        Intent intentMtp = new Intent();
        intentMtp.setAction("android.intent.action_MTP");
        PendingIntent pendingIntentMtp = PendingIntent.getBroadcast(mContext.getApplicationContext(),-1,intentMtp,PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.btMpt,pendingIntentMtp);

        Intent intentPtp = new Intent();
        intentPtp.setAction("android.intent.action_PTP");
        PendingIntent pendingIntentPtp = PendingIntent.getBroadcast(mContext.getApplicationContext(),-1,intentPtp,PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.btPtp,pendingIntentPtp);

        Intent intentCharg = new Intent();
        intentCharg.setAction("android.intent.action_CHAR");
        PendingIntent pendingIntentCharg = PendingIntent.getBroadcast(mContext.getApplicationContext(),-1,intentCharg,PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.btCharging,pendingIntentCharg);

        mNotification = builder.build();
        notificationManager.notify(1000,mNotification);
    }

    public  Notification.Builder getBuilder(String channel_id,String title,String context){
        return new Notification.Builder(mContext.getApplicationContext())
                .setAutoCancel(false)
                .setChannelId(channel_id)
                .setContentTitle(title)
                .setContentText(context)
                .setAutoCancel(true)
                .setWhen(0)
                .setOngoing(true)
                .setTicker("ticker")
                .setDefaults(0)
                .setSmallIcon(R.mipmap.ic_launcher);
    }


    private class MyNotificationReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
              if(intent!=null){
                  if("android.intent.action_MTP".equals(intent.getAction())){
                      mNotification.contentView.setTextColor(R.id.btMpt,mContext.getResources().getColor(R.color.colorPrimary));
                      mNotification.contentView.setTextColor(R.id.btPtp,mContext.getResources().getColor(R.color.default_bt));
                      mNotification.contentView.setTextColor(R.id.btCharging,mContext.getResources().getColor(R.color.default_bt));
                      notificationManager.notify(1000,mNotification);
                  }else if("android.intent.action_PTP".equals(intent.getAction())){
                      mNotification.contentView.setTextColor(R.id.btMpt,mContext.getResources().getColor(R.color.default_bt));
                      mNotification.contentView.setTextColor(R.id.btPtp,mContext.getResources().getColor(R.color.colorPrimary));
                      mNotification.contentView.setTextColor(R.id.btCharging,mContext.getResources().getColor(R.color.default_bt));
                      notificationManager.notify(1000,mNotification);
                  }else if("android.intent.action_CHAR".equals(intent.getAction())){
                      mNotification.contentView.setTextColor(R.id.btMpt,mContext.getResources().getColor(R.color.default_bt));
                      mNotification.contentView.setTextColor(R.id.btPtp,mContext.getResources().getColor(R.color.default_bt));
                      mNotification.contentView.setTextColor(R.id.btCharging,mContext.getResources().getColor(R.color.colorPrimary));
                      notificationManager.notify(1000,mNotification);
                  }
              }
        }
    }

}

 

第二部分布局xml文件notifaction_layout.xml:



   
       
       
       
   

 

第三部分用法:

MyNotification myNotification = new MyNotification(mContext);
myNotification.createCustomNotification(1,"10000","adbcdefg");
注:已适配8.0以上通知不显示问题。

 

 

 

你可能感兴趣的:(自定义notification布局,加仿消息通知悬浮效果,加通知中控件点击事件 刷新界面的代码实现。)