从Adapter类创建android通知

时间:2018-08-15 17:17:10

标签: adapter android-notifications countdowntimer

我有一个列表视图,其中每个项目都有一个倒计时。我想在特定时间到达时设置通知。

这是我通常设置通知的方式:

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this,  "")
            .setSmallIcon(R.drawable.refreshicon)
            .setContentTitle("Notification")
            .setContentText("Hurry up!")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT);
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

    notificationManager.notify(0, mBuilder.build());

这是我的适配器类:

public class TicketAdapter extends ArrayAdapter<TicketModel> implements View.OnClickListener{

    private ArrayList<TicketModel> dataSet;
    Context mContext;


    // View lookup cache
    private class ViewHolder {
        TextView txtName;
        TextView txtType;
        TextView txtTempsRestant;
        TextView txtDate;
        TextView txtSLA;
        ImageView info;
        RelativeLayout layout;
        boolean isSLAlate = false;

        Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {


                Bundle bundle = msg.getData();
                long timeLeftMS = bundle.getLong("time");
                String Nom = bundle.getString("name");

                int day = (int) ((timeLeftMS / (24*3600000)));
                int hour = (int) ((timeLeftMS / (1000*60*60)) % 24);
                int minute = (int) ((timeLeftMS / (60000)) % 60);
                int seconde = (int)timeLeftMS % 60000 / 1000;

                String timeLeftText = "";

                if (day<10) timeLeftText += "0";
                timeLeftText += day;
                timeLeftText += ":";
                if (hour<10) timeLeftText += "0";
                timeLeftText += hour;
                timeLeftText += ":";
                if (minute<10) timeLeftText += "0";
                timeLeftText += minute;
                timeLeftText += ":";
                if (seconde<10) timeLeftText += "0";
                timeLeftText += seconde;

                txtTempsRestant.setText(timeLeftText);

                if (timeLeftMS == getMSfromTime("00:00:05:00")){
                    // I want to set a notification right here !
                }


                info.setImageResource(getIconUrgence(timeLeftMS));


            }
        };

        Handler handlerLate = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                txtTempsRestant.setText("En retard");
                txtTempsRestant.setTextColor(Color.parseColor("#434343"));
                layout.setBackgroundColor(Color.parseColor("#3caa0000"));
                info.setImageResource(R.drawable.haute);
            }
        };

        Handler handlerAttente = new Handler(){
            @Override
            public void handleMessage(Message msg) {
             txtTempsRestant.setText("En attente...");
                txtTempsRestant.setTextColor(Color.parseColor("#434343"));
             layout.setBackgroundColor(Color.parseColor("#949494"));
                info.setImageResource(R.drawable.enattente);
            }
        };

        public void startTimer(long timeLeftMS, String statut, final String Nom) {
            if(statut.equals("4")){
                handlerAttente.sendEmptyMessage(0);
            }
            else{
                if (timeLeftMS<0){
                    handlerLate.sendEmptyMessage(0);
                }
                else{
                    CountDownTimer countDownTimer = new CountDownTimer(timeLeftMS, 1000) {

                        @Override
                        public void onTick(long l) {
                            Bundle bundle = new Bundle();
                            bundle.putLong("time", l);
                            bundle.putString("name", Nom);
                            Message message = new Message();
                            message.setData(bundle);
                            handler.sendMessage(message);
                        }

                        @Override
                        public void onFinish() {
                            handlerLate.sendEmptyMessage(0);
                        }
                    }.start();
                }
            }

        }

    }



    public TicketAdapter(ArrayList<TicketModel> data, Context context) {
        super(context, R.layout.row_item_ticket, data);
        this.dataSet = data;
        this.mContext=context;

    }


    @Override
    public void onClick(View v) {
        int position=(Integer) v.getTag();
        Object object= getItem(position);
        TicketModel TicketModel=(TicketModel)object;

        switch (v.getId())
        {
            case R.id.item_info:

                Snackbar.make(v, "is Late? : " +TicketModel.isTicketEnRetard(), Snackbar.LENGTH_LONG)
                        .setAction("No action", null).show();
                break;
        }
    }

    private int lastPosition = -1;


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // Get the data item for this position
        TicketModel TicketModel = getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
        final ViewHolder viewHolder; // view lookup cache stored in tag

        final View result;

        if (convertView == null) {

            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.row_item_ticket, parent, false);
            viewHolder.txtName = (TextView) convertView.findViewById(R.id.titreTV);
            viewHolder.txtDate = (TextView) convertView.findViewById(R.id.dateTV);
            viewHolder.txtSLA = (TextView) convertView.findViewById(R.id.slaTV);
            viewHolder.txtTempsRestant = (TextView) convertView.findViewById(R.id.SLARestantTV);
            viewHolder.info = (ImageView) convertView.findViewById(R.id.item_info);
            viewHolder.layout = (RelativeLayout) convertView.findViewById(R.id.backgroundRow);


            long timeLeft = Long.valueOf(TicketModel.getTempsRestantTicket());
            String Statut = TicketModel.getStatut();
            String Nom = TicketModel.getTitreTicket();


            result=convertView;
            //viewHolder.startTimer(Long.valueOf(TicketModel.getTempsRestantTicket()));
            viewHolder.startTimer(timeLeft, Statut, Nom);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
            result=convertView;
        }

        Animation animation = AnimationUtils.loadAnimation(mContext, (position > lastPosition) ? R.anim.up_from_bottom : R.anim.down_from_top);
        result.startAnimation(animation);
        lastPosition = position;

        viewHolder.txtName.setText(TicketModel.getTitreTicket());
        viewHolder.txtDate.setText(TicketModel.getDateTicket());
        viewHolder.txtSLA.setText(TicketModel.getSlaTicket());
        viewHolder.info.setOnClickListener(this);
        viewHolder.info.setTag(position);

        System.out.println("Here : "+TicketModel.getTitreTicket()); //getting each item's name
        System.out.println("Time = "+TicketModel.getTempsRestantTicket()); //getting each item's time left and it's correct
        System.out.println("Rebours = "+calculRebours(Long.valueOf(TicketModel.getTempsRestantTicket())));

        // Return the completed view to render on screen
        return convertView;
    }

        private long getMSfromTime(String timeLeftText) {
        long timeMS = 0;

        //"00:01:21"

        String days = timeLeftText.substring(0, 2);
        String hours = timeLeftText.substring(3, 5);
        String minutes = timeLeftText.substring(6, 8);
        String seconds = timeLeftText.substring(9, 11);

        //System.out.println("hours = "+hours+" | minutes = "+minutes+" | seconds = "+seconds);

        long daysMS = 24*60*60*1000*Long.valueOf(days);
        long hoursMS = 3600000*Long.valueOf(hours);
        long minutesMS = 60000*Long.valueOf(minutes);
        long secondsMS = 1000*Long.parseLong(seconds);
        timeMS = daysMS + hoursMS + minutesMS + secondsMS;

        return timeMS;
    }


}

我已经指定了要设置通知的位置。我不知道它是否无效,因为它是一个非活动类,我还尝试通过从MainActivity调用方法来设置通知,但没有

0 个答案:

没有答案