Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 防止周末发生重复闹钟大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个闹钟应用程序使用AlarmManager类,允许用户设置一次性闹钟或重复闹钟.我想扩展功能,以便用户可以将闹钟排除在外,例如在周末.

我已经把代码封锁了周末的闹钟到AlarmReceiver.java.@H_262_3@

>我不知道AlarmReceiver.java是否是在周末放置警报的代码的正确位置.
>我不知道我正在使用的代码在周末阻止闹钟是正确的.基本上我告诉AlarmReceiver如果今天是星期六或星期天什么都不做.否则,消除警报.@H_262_3@

AlarmActivity.java代码设置闹钟:@H_262_3@

//Set a one time alarm
            if (repeaTinterval == 0) {
                    alarmManager.set(AlarmManager.RTC,alarmTime.getTimeInMillis(),pendingIntent);
                    AlarmReceiver alarmReceiver = new AlarmReceiver(this); //https://stackoverflow.com/questions/16678763/the-method-getapplicationcontext-is-undefined

                    Toast.makeText(AlarmActivity.this,"Your one time reminder is Now set for " + hourSet + ":" + minuteSetString + amPmlabel,Toast
                            .LENGTH_LONG)
                            .show();
            }

            //Set a repeaTing alarm
            else {
                alarmManager.setRepeaTing(AlarmManager.RTC,repeaTintervalMilliseconds,pendingIntent);
                AlarmReceiver alarmReceiver = new AlarmReceiver(this); //https://stackoverflow.com/questions/16678763/the-method-getapplicationcontext-is-undefined

                    Toast.makeText(AlarmActivity.this,"Your reminder is Now set for " + hourSet + ":" + minuteSetString + amPmlabel + " and will " +
                            "repeat " +
                            "every " +
                            repeaTinterval + " minutes.",Toast.LENGTH_LONG).show();
        }

Alarmservice.Java:@H_262_3@

package com.joshbgold.move.BACkend;

import android.app.Intentservice;
import android.app.notificationmanager;
import android.app.pendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

import com.joshbgold.move.R;
import com.joshbgold.move.main.AlarmActivity;

public class Alarmservice extends Intentservice {
    private notificationmanager alarmnotificationmanager;

    public Alarmservice() {
        super("Alarmservice");
    }

    @Override
    public void onHandleIntent(Intent intent) {

            sendNotification("Move reminder");

    }

    private void sendNotification(String msg) {
        alarmnotificationmanager = (notificationmanager) this
                .getSystemservice(Context.NOTIFICATION_serviCE);

        PendingIntent contenTintent = PendingIntent.getActivity(this,new Intent(this,AlarmActivity.class),0);

        NotificationCompat.builder alarmNotificationBuilder = new NotificationCompat.builder(
                this).setContenttitle("Reminder").setsmallIcon(R.mipmap.ic_launcher)
                .setStyle(new NotificationCompat.bigTextStyle().bigText(msg))
                .setContentText(msg);


        alarmNotificationBuilder.setContenTintent(contenTintent);
        alarmnotificationmanager.notify(1,alarmNotificationBuilder.build());
    }

}

AlarmReceiver.Java:@H_262_3@

package com.joshbgold.move.BACkend;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.content.WakefulBroadcastReceiver;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class AlarmReceiver extends WakefulBroadcastReceiver {

    Context myContext;
    public AlarmReceiver(Context context){
        myContext = context;
    }

    public AlarmReceiver(){

    }

    //get the current day
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE");
    Date date = new Date();
    String dayOfTheWeek = simpleDateFormat.format(datE);

    Calendar calendar = Calendar.geTinstance();
    int currentHour = calendar.HOUR_OF_DAY;

    Boolean Noweekends = true;
    Boolean workHoursOnly = true;

    @Override
    public void onReceive(final Context context,Intent intent) {


        try {  //this value Could be null if user has not set it...
            Noweekends = loadPrefs("Noweekends",Noweekends);
            workHoursOnly = loadPrefs("workHoursOnly",workHoursOnly);
        } catch (Exception E) {
            e.printStackTrace();
        }


        if(dayOfTheWeek == "Saturday" || dayOfTheWeek == "Sunday"  && Noweekends == truE) {
            //Alarm is not wanted on the weekend
            try {
                wait(1);  //waits for one-thousandth of a millisecond
            } catch (InterruptedException E) {
                e.printStackTrace();
            }
        }

        else if ((currentHour < 9 || currentHour > 17)  && workHoursOnly == truE){
            //Alarm outside of work hours
            try {
                wait(1);  //waits for one-thousandth of a millisecond
            } catch (InterruptedException E) {
                e.printStackTrace();
            }
        }

        else {

            Intent myIntent = new Intent();
            myIntent.setClassName("com.joshbgold.move","com.joshbgold.move.main.ReminderActivity");
            myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(myIntent);
        }
    }

    //get prefs
    private Boolean loadPrefs(String key,Boolean value) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(myContext);
        Boolean data = sharedPreferences.getBoolean(key,value);
        return data;
    }
}

AlarmReceiver.java(更正后的代码)@H_262_3@

package com.joshbgold.move.BACkend;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.content.WakefulBroadcastReceiver;
import java.util.Calendar;

public class AlarmReceiver extends WakefulBroadcastReceiver {

    Context myContext;
    public AlarmReceiver(Context context){
        myContext = context;
    }

    public AlarmReceiver() {

    }

    private Boolean workHoursOnly = false;
    private Boolean Noweekends = false;

    @Override
    public void onReceive(final Context context,Intent intent) {

        Calendar calendar = Calendar.geTinstance();
        int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
        int today = calendar.get(Calendar.DAY_OF_WEEK);
        Boolean isWeekend = (today == Calendar.SUNDAY) || (today == Calendar.SATURDAY);
        Boolean isOutsideWorkHours = (currentHour < 9) || (currentHour > 16);

        //checkPrefs checks whether a preferences key exists
        if (checkPrefs("workHoursOnlyKey")){
            workHoursOnly = loadPrefs("workHoursOnlyKey",workHoursOnly);
        }

        if(checkPrefs("NoweekendsKey")){
            Noweekends = loadPrefs("NoweekendsKey",Noweekends);
        }

        /* try {  //this value Could be null if user has not set it...
            workHoursOnly = loadPrefs("workHoursOnly",workHoursOnly);
        } catch (Exception E) {
            e.printStackTrace();
        }
       */

        /*try {  //this value Could be null if user has not set it...
        Noweekends = loadPrefs("Noweekends",Noweekends);
        } catch (Exception E) {
            e.printStackTrace();
        }*/

        if(isWeekend && Noweekends) {
            //Alarm is not wanted on the weekend
            try {
                Thread.sleep(1);  //waits for millisecond
            } catch (InterruptedException E) {
                e.printStackTrace();
            }
        }

        else if (isOutsideWorkHours  && workHoursOnly){
            //Alarm not wanted outside of work hours
            try {
                Thread.sleep(1);  //waits for millisecond
            } catch (InterruptedException E) {
                e.printStackTrace();
            }
        }

        else {
            //Alarm is wanted,and should go off
            Intent myIntent = new Intent();
            myIntent.setClassName("com.joshbgold.move","com.joshbgold.move.main.ReminderActivity");
            myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(myIntent);
        }
    }

    //check if a prefs key exists
    private Boolean checkPrefs(String key){
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(myContext);
        Boolean exists = sharedPreferences.contains(key);
        return exists;
    }

    //get prefs
    private Boolean loadPrefs(String key,value);
        return data;
    }
}

解决方法@H_675_25@
你的做法很好反正我建议你避免使用硬编码的字符串进行周末检查.您已经在代码中定义了一个Calendar对象,只需使用它:
Calendar calendar = Calendar.geTinstance();
//..
int today  = calendar.get(Calendar.DAY_OF_WEEK);
Boolean isWeekend = (today == Calendar.SUNDAY) || (today == Calendar.SATURDAY);

并相应更新您的代码:@H_262_3@

//...
  int today  = calendar.get(Calendar.DAY_OF_WEEK);
  Boolean isWeekend = (today == Calendar.SUNDAY) || (today == Calendar.SATURDAY);
  if(isWeekend && Noweekends == truE) {
        //Alarm is not wanted on the weekend
        try {
            Thread.sleep(1);  
        } catch (InterruptedException E) {
            e.printStackTrace();
        }
    }
  //...

大佬总结

以上是大佬教程为你收集整理的android – 防止周末发生重复闹钟全部内容,希望文章能够帮你解决android – 防止周末发生重复闹钟所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。