团队冲刺第十天-KeepRunningAPP

摘要:今天主要解决了昨天的问题,问题一:当退出APP再进入时,所有的闹钟都变成了关闭状态。这个问题的原因是代码中显示开关按钮状态部分的代码逻辑出现了问题,重新换了个思路后就轻松解决了这个问题。还有就是长按删除的问题,虽然数据可以删除,但是没有解决页面刷新的问题,目前正在找可以在apapter里刷新list的解决方法。除了解决这俩问题,另外又美化了一些界面,并把队友做的音乐播放部分的内容添加到了整个项目中。下面是主要代码和截图。

package com.alarm;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import com.example.keeprunning1.R;

import java.util.Calendar;
import java.util.List;

import static android.content.Context.ALARM_SERVICE;


public class AlarmAdapter extends BaseAdapter {
    private List<Alarm> mlist;
    private Context mContext;
    private LayoutInflater mlayoutInflater;
    private AlarmOperator malarmOperator;
    Calendar calendar;

    public AlarmAdapter(Context context, List<Alarm> list) {
        mContext = context;
        mlist = list;
        mlayoutInflater = LayoutInflater.from(context);
        malarmOperator = new AlarmOperator(context);
    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = mlayoutInflater.inflate(R.layout.alarmitem, null);
            viewHolder.mHour = convertView.findViewById(R.id.hour);
            viewHolder.mMinute = convertView.findViewById(R.id.minute);
            viewHolder.mContent = convertView.findViewById(R.id.content_item);
            viewHolder.mNet=convertView.findViewById(R.id.net);
            viewHolder.mClockType = convertView.findViewById(R.id.switch_control);
            viewHolder.todetail = convertView.findViewById(R.id.todetail);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final Alarm bean = mlist.get(position);
        final String name = bean.getUsername();
        final String hour = bean.getHour();
        final String minute = bean.getMinute();
        Log.i("name", name);
        int clockType = bean.getClockType();
        Log.d("volley", String.valueOf(clockType));
        if (clockType == 1) {
            viewHolder.mClockType.setChecked(true);
        } else if (clockType == 0) {
            viewHolder.mClockType.setChecked(false);
            viewHolder.mHour.setTextColor(mContext.getResources().getColor(R.color.colorGray));
            viewHolder.mMinute.setTextColor(mContext.getResources().getColor(R.color.colorGray));
            viewHolder.mNet.setTextColor(mContext.getResources().getColor(R.color.colorGray));
            viewHolder.mContent.setTextColor(mContext.getResources().getColor(R.color.colorGray));
        }

        viewHolder.todetail.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setMessage("确定删除?");
                builder.setTitle("提示");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        malarmOperator.deletebyuser(name, hour, minute);
                        //notifyDataSetChanged();
                        AlarmManager am;
                        Intent intent1 = new Intent(mContext, CallAlarm.class);
                        PendingIntent sender1=PendingIntent.getBroadcast(
                                mContext,0, intent1, 0);
                        am =(AlarmManager)mContext.getSystemService(ALARM_SERVICE);
                        am.cancel(sender1);
                        Toast.makeText(mContext, "删除成功", Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.create().show();
                return true;
            }
        });
        viewHolder.mClockType.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    malarmOperator.updateswitch(name, hour, minute);
//                    viewHolder.mClockType.setChecked(true);
                    Toast.makeText(mContext, "开启闹钟", Toast.LENGTH_SHORT).show();
                    viewHolder.mHour.setTextColor(mContext.getResources().getColor(R.color.colorBlack));
                    viewHolder.mMinute.setTextColor(mContext.getResources().getColor(R.color.colorBlack));
                    viewHolder.mNet.setTextColor(mContext.getResources().getColor(R.color.colorBlack));
                    viewHolder.mContent.setTextColor(mContext.getResources().getColor(R.color.colorBlack));
                    Intent intent = new Intent(mContext, CallAlarm.class);
                    PendingIntent sender = PendingIntent.getBroadcast(
                            mContext, 0, intent, 0);
                    AlarmManager am;
                    //使用闹钟服务
                    am = (AlarmManager) mContext.getSystemService(ALARM_SERVICE);
                    calendar = Calendar.getInstance();
                    calendar.setTimeInMillis(System.currentTimeMillis());
                    calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(bean.getHour()));
                    calendar.set(Calendar.MINUTE, Integer.parseInt(bean.getMinute()));
                    calendar.set(Calendar.SECOND, 0);
                    calendar.set(Calendar.MILLISECOND, 0);
                    Log.e("TAG", calendar.getTimeInMillis() + "");
                    Log.e("TAG", System.currentTimeMillis() + "");
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                        if (System.currentTimeMillis() > calendar.getTimeInMillis() + 40000) {
                            //加24小时
                            am.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis() + 86400000, sender);
                        } else {
                            am.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
                        }
                    }
                } else if (!isChecked) {
                    malarmOperator.updateswitch1(name, hour, minute);
                    viewHolder.mHour.setTextColor(mContext.getResources().getColor(R.color.colorGray));
                    viewHolder.mMinute.setTextColor(mContext.getResources().getColor(R.color.colorGray));
                    viewHolder.mNet.setTextColor(mContext.getResources().getColor(R.color.colorGray));
                    viewHolder.mContent.setTextColor(mContext.getResources().getColor(R.color.colorGray));
                    Intent intent = new Intent(mContext, CallAlarm.class);
                    PendingIntent sender = PendingIntent.getBroadcast(
                            mContext, 0, intent, 0);
                    AlarmManager am;
                    am = (AlarmManager) mContext.getSystemService(ALARM_SERVICE);
                    am.cancel(sender);
                    Toast.makeText(mContext, "关闭闹钟", Toast.LENGTH_SHORT).show();

                }
            }
        });
        viewHolder.mContent.setText(bean.content);
        viewHolder.mHour.setText(bean.hour+"");
        viewHolder.mMinute.setText(bean.minute+"");
        return convertView;
    }
    public class ViewHolder {
        public TextView mHour;
        public TextView mMinute;
        public TextView mContent;
        public TextView mNet;
        Switch mClockType;
        LinearLayout todetail;
        //public CheckBox mCheckBox;
    }
}
AlarmAdapter

 

posted @ 2020-04-24 22:25  MoooJL  阅读(189)  评论(0编辑  收藏  举报