2

I am just starting out with android and ant is giving me the following error:

-compile: java:44: cannot find symbol
[javac] symbol  : method getSystemService(java.lang.String)
[javac] AudioManager am = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
                                            ^

I don't understand the problem since I am importing android.media.AudioManager;

My code is as follows:

package com.example.findme;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
import android.media.AudioManager;

public class SMSReceiver extends BroadcastReceiver
{
public final static String EXTRA_MESSAGE = "com.example.RemoVol.MESSAGE";
public final static String SILENT_MESSAGE = "Your phone has been silenced";
public final static String VOLUME_MESSAGE = "Your phone volume is normal";

@Override   
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
    Bundle bundle = intent.getExtras();
    SmsMessage msgs[] = null;
    String str ="";
    String testString="";   
    if (bundle != null)
    {
        //---retrieve the SMS message received---
        Object pdus[]=(Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];
        for (int i=0; i<msgs.length; i++)
            {
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                str += "SMS from" + msgs[i].getOriginatingAddress();
                str += " :";
                str +=msgs[i].getMessageBody().toString();
                testString=msgs[i].getMessageBody().toString(); 
                str += "\n";
            }
    //---display the new SMS message---
    Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
        if(testString.equals("#silent"))
        {   
            intent.putExtra(EXTRA_MESSAGE, SILENT_MESSAGE);
            AudioManager am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
            am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
        }   
        else if(testString.equals("#volume"))
        {   
        AudioManager am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
        am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        intent.putExtra(EXTRA_MESSAGE, VOLUME_MESSAGE);
        }
    }
}
}

I am compiling using the command "ant debug" in the root of my android project.

I hope this isn't really something stupid and obvious. Apologies in advance if I have wasted your time.

Thank you for reviewing this.

shas
  • 703
  • 2
  • 8
  • 31
user614885
  • 141
  • 2
  • 2
  • 9
  • The "^" symbol in the error output is under the "." in the statement Audiomanager am = this.(this period right here is where it is under)getSystemService(Context.AUDIO_SERVICE); – user614885 Sep 23 '12 at 20:08
  • I keep trying to edit this so the ^ symbol is in the correct spot. It is not working. I am sorry. – user614885 Sep 23 '12 at 20:13

1 Answers1

6

You need to pass in the context in which the BroadcastReciever is supplying (you can't use this)

//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
if(testString.equals("#silent"))
{   
    intent.putExtra(EXTRA_MESSAGE, SILENT_MESSAGE);
    AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}   
else if(testString.equals("#volume"))
{   
    AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
    intent.putExtra(EXTRA_MESSAGE, VOLUME_MESSAGE);
}
mrres1
  • 1,147
  • 6
  • 10
  • That worked! Thank you. I am a little confused though. If I need to supply a context. What does "this" refer to? – user614885 Sep 23 '12 at 20:55
  • "this" is a Java keyword that refers to the current class instance you are in. [http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html](http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html) – mrres1 Sep 23 '12 at 22:44