获得一个找不到符号:从蚂蚁的方法getSystemService [英] Getting a cannot find symbol : method getSystemService from ant

查看:1409
本文介绍了获得一个找不到符号:从蚂蚁的方法getSystemService的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始接触Android和蚂蚁给我下面的错误:

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

-compile:VA:44:找不到符号
[javac的]符号:方法getSystemService(java.lang.String中)
[javac的] AudioManager上午=(AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
                                            ^

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

因为我进口android.media.AudioManager我不明白的问题;

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

我的code是如下:

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

我在我的Andr​​oid项目的根目录下使用命令蚁调试编译。

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.

推荐答案

您需要在其中BroadcastReciever正在提供上下文传递(你不能使用这个

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

这篇关于获得一个找不到符号:从蚂蚁的方法getSystemService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆