2016-06-29 3 views
1

私が取り組んでいるアプリケーションにアラームを追加しようとしていますが、何らかの愚かな理由のためにアラームが鳴っても、アプリ。アラームはアプリケーションを殺さずに鳴るのを止めることはありません

アラームはToggleButtonによってアクティブになります。それがオンになっていると、予定された時刻にアラームが鳴り、オフになっても、それは...または少なくとも想定されません。

ここではトグルボタンのonclickコードです:私は解決策であるため作ってみた

public void onReceive(Context context, Intent intent) { 
     //this will update the UI with message 
     RemindersActivity inst = RemindersActivity.instance(); 
     //Sets the content of AlarmText in RemindersFragment 
     inst.setAlarmText("Alarm! Wake up! Wake up!"); 

     //this will sound the alarm tone 
     //this will sound the alarm once, if you wish to 
     //raise alarm in loop continuously then use MediaPlayer and setLooping(true) 
     Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); 
     if (alarmUri == null) { 
      alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     } 
     Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri); 
     ringtone.play(); 

     ComponentName comp = new ComponentName(context.getPackageName(), 
      AlarmService.class.getName()); 
     startWakefulService(context, (intent.setComponent(comp))); 
     setResultCode(Activity.RESULT_OK); 
    } 

答えて

0

@OnClick(R.id.alarmToggle2) 
protected void onAlarmToggleClicked(){ 
    if(alarmToggle.isChecked()){ 
     alarmTimeText.setTextColor(Color.GREEN); 
     alarmTimeText.setVisibility(View.VISIBLE); 
     //Switch on the alarm 
     Calendar calendar = Calendar.getInstance(); 
     calendar.setTimeInMillis(System.currentTimeMillis()); 
     calendar.set(Calendar.HOUR_OF_DAY, alarmHour); 
     calendar.set(Calendar.MINUTE, alarmMinute); 

     //It's now time to set off the alarm 
     Log.d(TAG, "ALARM"); 
     alarmReceiverIntent = new Intent(RemindersActivity.instance(), 
       AlarmReceiver.class); 
     broadcastIntent = PendingIntent.getBroadcast(getContext(), alarmId, 
       alarmReceiverIntent, 0); 
     //RTC means that the alarm won't be used if the device is asleep, RTC_WAKEUP means 
     //that it will. 
     alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), broadcastIntent); 
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
       AlarmManager.INTERVAL_DAY, broadcastIntent); 
    } else { 
     Log.d(TAG, "Cancel alarm"); 
     alarmTimeText.setVisibility(View.INVISIBLE); 
     alarmManager.cancel(broadcastIntent); 
     alarmText.setText(""); 
    } 
} 

そして、ここではAlarmReceiverクラスのonReceive()メソッドのコードです着信音はAlarmReceiverの静的変数になりました。これがアラーム音を止める方法です:

if(!alarmToggle.isChecked()) { 
    //Stop the alarm from ringing 
    if(AlarmReceiver.ringtone.isPlaying()) 
     AlarmReceiver.ringtone.stop(); 
    alarmTimeText.setVisibility(View.INVISIBLE); 
    alarmManager.cancel(broadcastIntent); 
    alarmText.setText(""); 
} 
関連する問題