2

我刚开始使用 android 和 ant 给我以下错误:

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

我不明白这个问题,因为我正在导入android.media.AudioManager;

我的代码如下:

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

我在我的 android 项目的根目录中使用命令“ant debug”进行编译。

我希望这不是真的愚蠢和明显的事情。如果我浪费了您的时间,请提前道歉。

感谢您对此进行审查。

4

1 回答 1

6

您需要传入 BroadcastReciever 提供的上下文(您不能使用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);
}
于 2012-09-23T20:19:37.523 回答