Android AlarmManager and Service question

2019-03-03 02:11发布

问题:

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:

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();
}

AlarmReceiver.java:

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

Important part of maincode.java:

    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() >= 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();
                }
        }

        });

(rem1 is the reminder button whose text depends on the name of anything the user wants)

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?

回答1:

No you can't do that directly from listener. You can disable the alarm this way:

Intent intent = new Intent(maincode.this, AlarmReceiver.class);
PendingIntent pendingIntent =  PendingIntent.getBroadcast(bInsulinReminder.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
pendingItem.cancel();
alarmManager.cancel(pendingItem);

Or if (I suppose) AlarmReceiver is implementation of BroadcastReceiver and from onReceive method you start your MyService which is implementation of Service class.

So, if you want to stop this alarm from inside of your maincode.java listener you can just stop MyService by recreating PendingIntent you used in AlarmReceiver and executing stopService method.

Hope that helps.