Question

I am trying to build an alarm application. When the alarm turns on, the user has to scan a matching QR code before it is turned off. I've taken a look at this link to get the sound playing: How to play ringtone/alarm sound in Android and I am using the ScanningViaIntent from the zxing library for the QR code scanner: https://code.google.com/p/zxing/.

So I start the sound in the onStart() activity:

@Override
public void onStart(){
    super.onStart();
    Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    r = RingtoneManager.getRingtone(getApplicationContext(), notification);
    r.play();
}

The user then starts the scanner by pressing a button:

private class HandleClick implements OnClickListener{
    public void onClick(View arg0) {
        IntentIntegrator integrator = new IntentIntegrator(AlarmRequirementsActivity.this);
        integrator.initiateScan();

    }
}

The result of the scanner is returned here:

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
    if (scanResult != null) {
        System.out.println("scanREsult" + scanResult);
        System.out.println("requestCode:  " + requestCode);
        TextView result =(TextView)findViewById(R.id.scanResult);       
        if (resultCode == RESULT_OK) {
            String scanResultString = intent.getStringExtra("SCAN_RESULT");

            if(scanResultString .equals(matchString))
            {
                result.setText("You found it!");        
                r.stop();
            }
            else
            {
                result.setText("\"" + scanResultString  + "\""+ " did not match");                  
            }

            System.out.println(intent.getStringExtra("SCAN_RESULT"));


        } else if (resultCode == RESULT_CANCELED) {
        }

    }
    // else continue with any other code you need in the method

}

As you can see, I call r.stop() after a successful match. However these are my problems:

  • The activity is restarted after coming back from the scanner. It doesn't matter if the match was successful or not.
  • This results in two alarm tones being played now

I've tried putting it in the onCreate() method but to no avail as well.

UPDATE:

I've tried:

@Override
public void onStart(){
    super.onStart();
    Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    r = RingtoneManager.getRingtone(getApplicationContext(), notification);
    if(!r.isPlaying())
    {
        r.play();           
    }
}

and this below. Both of which with the same problems

if(scanResultString .equals(matchString))
{
    result.setText("You found it!");
    if(r.isPlaying())
    {
        r.stop();
    }
}
Was it helpful?

Solution

The activity is restarted after coming back from the scanner. It doesn't matter if the match was successful or not.

I assume that you need to start another activity to do the scan, which means that your activity will (at least) need to be paused and more likely stopped to allow that other activity to run (as per the Android activity lifecycle).

Therefore, you will have to expect onStart() to be called when returning from the scanner.

This results in two alarm tones being played now

You should be able to avoid this and your code to check if the ringtone is already playing seems like a good start. However, I suspect you are creating a new ringtone object each time onStart() is executed.

It is hard for me to guess at all of the things you will need to do to fully resolve your problems (not to mention problems you will only see when your activity is fully recreated by Android - for example when the screen orientation changes - as this needs further handling in your code; see the Android doc for the activity lifecycle, particularly onSaveInstanceState()).

My guess at the next step would be to move the line:

r = RingtoneManager.getRingtone(getApplicationContext(), notification);

into your onCreate() method. My hope is that this, combined with the if (!r.isPlaying()) code should prevent the double-alarm issue in most cases.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top