自定義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:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
   <RelativeLayout
       android:id="@+id/rlNotiBg"
       android:layout_width="match_parent"
       android:layout_height="120dp"
       >
       <TextView
           android:id="@+id/btMpt"
           android:layout_width="130dp"
           android:layout_height="50dp"
           android:layout_alignParentLeft="true"
           android:layout_alignParentBottom="true"
           android:text="傳文件"
           />
       <TextView
           android:id="@+id/btPtp"
           android:layout_width="130dp"
           android:layout_height="50dp"
           android:layout_centerInParent="true"
           android:layout_alignParentBottom="true"
           android:text="看照片"
           android:textColor="@color/colorPrimary"
           />
       <TextView
           android:id="@+id/btCharging"
           android:layout_width="130dp"
           android:layout_height="50dp"
           android:layout_alignParentRight="true"
           android:layout_alignParentBottom="true"
           android:text="僅充電"
           android:textColor="@color/colorPrimary"
           />
   </RelativeLayout>
</LinearLayout>

 

第三部分用法:

MyNotification myNotification = new MyNotification(mContext);
myNotification.createCustomNotification(1,"10000","adbcdefg");
注:已適配8.0以上通知不顯示問題。

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章