2015-10-15 5 views
10

पर होने वाले अलार्म को दोहराने से रोकें मेरे पास अलार्म मैनेजर क्लास का उपयोग करके अलार्म ऐप है जो उपयोगकर्ता को एक बार अलार्म या दोहराने वाला अलार्म सेट करने की अनुमति देता है। मैं क्षमताओं का विस्तार करना चाहता हूं ताकि उपयोगकर्ता अलार्म को बाहर जाने से बाहर कर सके, उदाहरण के लिए, सप्ताहांत पर।सप्ताहांत

मैंने सप्ताहांत पर अलार्म को AlarmReceiver.java में अवरुद्ध करने के लिए कोड डाला है।

  1. मैं अगर AlarmReceiver.java कोड डाल करने के लिए सही जगह है यकीन नहीं है कि सप्ताह के अंत में ब्लॉक अलार्म।
  2. मुझे यकीन नहीं है कि अगर मैं सप्ताहांत पर अलार्म को अवरुद्ध करने के लिए उपयोग कर रहा हूं तो कोड सही है। असल में मैं अलार्म रिसीवर को कुछ भी नहीं करने के लिए कहता हूं अगर आज शनिवार या रविवार है। अन्यथा, अलार्म से आग लगाना।

AlarmActivity.java कोड है कि अलार्म सेट:

//Set a one time alarm 
      if (repeatInterval == 0) { 
        alarmManager.set(AlarmManager.RTC, alarmTime.getTimeInMillis(), pendingIntent); 
        AlarmReceiver alarmReceiver = new AlarmReceiver(this); //http://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, alarmTime.getTimeInMillis(), repeatIntervalMilliseconds, pendingIntent); 
       AlarmReceiver alarmReceiver = new AlarmReceiver(this); //http://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:

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, 0, 
       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:

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 (सुधारा कोड)

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,boolean value) { 
     SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(myContext); 
     boolean data = sharedPreferences.getBoolean(key, value); 
     return data; 
    } 
} 
+0

कुल निट उठा। प्रतीक्षा करें (1) एक मिलीसेकंड है, एक मिलीसेकंड का एक हजारवां नहीं। – dnellis74

+0

क्या गलत हो रहा है? क्या आपने इसे सप्ताहांत पर परीक्षण किया था और अलार्म निकाल दिया गया था? क्या आपने इसे एक सप्ताहांत में डीबग किया था और यह देखने के लिए "dayOfTheWeek" के मूल्य की जांच की है कि इसके अंदर क्या है? दोहरानाIntervalMilliseconds दोहराने का मूल्य क्या है? – Christian

उत्तर

7

आपका दृष्टिकोण ठीक है।वैसे भी मैं आपको पर सुझाव देता हूं सप्ताहांत चेक के लिए हार्डकोडेड तारों का उपयोग करने से बचें। आप पहले से ही एक Calendar वस्तु अपने कोड में परिभाषित किया गया है, बस इसे का उपयोग करें:

Calendar calendar = Calendar.getInstance(); 
//.. 
int today = calendar.get(Calendar.DAY_OF_WEEK); 
boolean isWeekend = (today == Calendar.SUNDAY) || (today == Calendar.SATURDAY); 

और तदनुसार अपना कोड अपडेट:

//... 
    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(); 
     } 
    } 
    //... 
+0

मैं अलार्म अनुरोध को बंद करने में सफलतापूर्वक सक्षम हूं, और फिर ऊपर दिए गए कोड का उपयोग करके अलार्म रिसीवर में अनुरोध को फ़िल्टर कर रहा हूं। मैंने त्रुटि के कारण प्रतीक्षा() विधि को Thread.sleep में बदल दिया "IllegalMonitorStateException: ऑब्जेक्ट प्रतीक्षा से पहले थ्रेड द्वारा लॉक नहीं किया गया है()"। अगर परीक्षण के लिए हालत है तो यह एक डमी स्थापित करता है क्योंकि यह सप्ताहांत नहीं है। पहले जब किसी भी स्थिति में सत्य थे, ऐप क्रैश हो रहा था, लेकिन अभी ठीक है। – joshgoldeneagle

+0

साझा किए गए संदर्भों का उपयोग करने के लिए प्रयास करें, साझा किए गए संदर्भों का उपयोग करने के लिए साझा करें संदर्भ साझा करें, साझा किए गए संदर्भ विधि के अस्तित्व की जांच करने के लिए() साझा किया गया है। – joshgoldeneagle

+0

साझा किए गए संदर्भों के साथ मैंने पाया एक और मुद्दा यह है कि चूंकि अलार्म रिसीवर जागरूक हो जाता है ब्रॉडकास्ट रिसीवर, यह स्वचालित रूप से एप्लिकेशन संदर्भ प्राप्त नहीं करता है। मुझे मुख्य संदर्भ से मुख्य संदर्भ से अनुप्रयोग संदर्भ में पास करना होगा, और ऑनसेसिव विधि से साझा किए गए व्यवहार तरीकों से – joshgoldeneagle

0

पूरी तरह से स्पिटबॉलिंग, मैं एंड्रॉइड डेवलपर नहीं हूं। सप्ताह का आपका दिन परिवर्तनीय:

Date date = new Date(); 
String dayOfTheWeek = simpleDateFormat.format(date); 

कक्षा का सदस्य चर है। यह केवल तब ही सेट हो जाएगा जब वर्ग तत्काल हो। अगर यह केवल स्टार्टअप चक्र के किसी प्रकार में हो रहा है, तो दिन कभी भी बदल नहीं पाएगा। तो अगर मैं सही हूं, अगर आप बुधवार को अपना अलार्म सेट करते हैं, तो यह कोड हमेशा शनिवार को भी बुधवार को सोचता है।

वास्तविक कार्य के अंदर कोड की उन दो पंक्तियों को रेसिव() पर ले जाएं और मैं शर्त लगाऊंगा कि सप्ताह के उस दिन को उठाना शुरू हो जाएगा।

3

dnellis74 के जवाब के अलावा आप भी यहाँ कुछ कोष्ठक की जरूरत है:

if(dayOfTheWeek == "Saturday" || dayOfTheWeek == "Sunday" && noWeekends == true) 

भी

if((dayOfTheWeek == "Saturday" || dayOfTheWeek == "Sunday") && noWeekends == true) 

हो जाता है मुझे लगता है कि आप की जरूरत नहीं है

try { 
     wait(1); //waits for one-thousandth of a millisecond 
    } catch (InterruptedException e) { 
       e.printStackTrace(); 
    } 
0

1 ।) यह पूरी तरह से है एक घटना को आग लगाने के लिए ठीक है और फिर इसे एक ग्राहक (ब्रॉडकास्ट रिसीवर) में फ़िल्टर/संसाधित करें। यही वह है जिसके लिए वे बने हैं।

2.)

if((dayOfTheWeek == "Saturday" || dayOfTheWeek == "Sunday") && noWeekends == true) 

नोटिस कोष्ठकों को ठीक।

संबंधित मुद्दे