摘要:今天完成了整个闹钟功能的实现,但是整体来说还有几个细节性的问题,比如长按删除闹钟会弹出两次提示框,还有就是当退出APP再进入时,所有的闹钟都变成了关闭状态,估计是哪里的细节出现了问题,但是总的来说今天还是挺有成就的。明天的工作就只剩解决这两个问题了。下面是主要代码以及运行效果图。
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); } public void refresh(List<Alarm> list){ mlist=list; notifyDataSetChanged(); } @Override public void notifyDataSetChanged() { super.notifyDataSetChanged(); } @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.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); 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(); 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.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); } } // calendar = Calendar.getInstance(); // int mhour = calendar.get(Calendar.HOUR_OF_DAY); // int mminute = calendar.get(Calendar.MINUTE); // Log.d("Hour", String.valueOf(mhour)); // Log.d("Minute", String.valueOf(mminute)); // final MediaPlayer mMediaPlayer; // if(bean.getHour().equals(mhour)&&bean.getMinute().equals(mminute)){ // mMediaPlayer= MediaPlayer.create(mContext, R.raw.alarmmusic); // mMediaPlayer.start(); // new AlertDialog.Builder(mContext) // .setIcon(R.drawable.clock) // .setTitle("闹钟响了") // .setCancelable(false) // .setMessage("时间到了!") // .setPositiveButton("关掉" // , new DialogInterface.OnClickListener() { // @Override // public void onClick(DialogInterface dialog, int which) { // mMediaPlayer.stop(); // } // }).show(); // } }else if(!isChecked){ malarmOperator.updateswitch1(name,hour,minute); viewHolder.mClockType.setChecked(false); 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.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); refresh(mlist); Toast.makeText(mContext, "删除成功", Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.create().show(); return false; } }); // int clockType=bean.getClockType(); // Log.d("volley", String.valueOf(clockType)); // if(clockType==1){ // viewHolder.mClockType.setChecked(true); // } // if(clockType==0){ // viewHolder.mClockType.setChecked(true); // } 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 Switch mClockType; LinearLayout todetail; //public CheckBox mCheckBox; } }
package com.alarm; import android.app.Activity; import android.content.DialogInterface; import android.media.MediaPlayer; import android.os.Bundle; import androidx.appcompat.app.AlertDialog; import com.example.keeprunning1.R; public class AlarmAlert extends Activity { private MediaPlayer mediaPlayer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); int position = getIntent().getIntExtra("position",-1); mediaPlayer = MediaPlayer.create(this, R.raw.alarmmusic); mediaPlayer.start(); new AlertDialog.Builder(AlarmAlert.this) .setIcon(R.drawable.clock) .setTitle("闹钟响了") .setCancelable(false) .setMessage("时间到了!") .setPositiveButton("关掉" , new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { AlarmAlert.this.finish(); mediaPlayer.stop(); } }).show(); } }