Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid AlarmManager and Service question
    primarykey
    data
    text
    <p>There are some files in my app, but only 3 of them is important now. It's a reminder app with alarm sound and notifications. I hava a maincode.java file containing a checkbox and its listener. If user checks in the chechbox an AlarmManager sends an intent to AlarmReceiver.java, which starts MyService.java. MyService java contains code about playing sound. Code is partial. MyService.java:</p> <pre><code>public void onCreate() { Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show(); Log.d(TAG, "onCreate"); player = MediaPlayer.create(this, R.raw.sound); player.setLooping(false); // Set looping } @Override public void onDestroy() { Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show(); Log.d(TAG, "onDestroy"); player.stop(); } @Override public void onStart(Intent intent, int startid) { Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show(); Log.d(TAG, "onStart"); player.start(); } </code></pre> <p>AlarmReceiver.java:</p> <pre><code>public void onCreate() { Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show(); Log.d(TAG, "onCreate"); player = MediaPlayer.create(this, R.raw.sound); player.setLooping(false); // Set looping </code></pre> <p>Important part of maincode.java:</p> <pre><code> cb1 = (CheckBox) findViewById(R.id.CheckBox01); cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (cb1.isChecked()) { if (GlobalVars.getHourOfDay() &gt;= 0) { Toast.makeText(maincode.this, "ok", Toast.LENGTH_SHORT).show(); rem1.setText(GlobalVars.getReminder1name()); Intent intent = new Intent(maincode.this, AlarmReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(bInsulinReminder.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, GlobalVars.getHourOfDay()); cal.set(Calendar.MINUTE, GlobalVars.getMinute()); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis()+ 3000, 6000, pendingIntent); } Toast.makeText(maincode.this, "Checked", Toast.LENGTH_SHORT).show(); } else { rem1.setText("No reminder set"); Toast.makeText(maincode.this, "Not checked", Toast.LENGTH_SHORT).show(); } } }); </code></pre> <p>(rem1 is the reminder button whose text depends on the name of anything the user wants)</p> <p>Problem with the code is that if i start the alarm, i cannot stop it. I know there is the player.stop() command in MyService.java, but how can i call it from the end of maincode.java where the checkbox is unchecked?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload