【问题标题】:Android Studio - Getting the Scope RightAndroid Studio - 获得正确的范围
【发布时间】:2021-02-10 16:33:53
【问题描述】:

如果我在这里没有得到我所有的术语,请原谅我:我仍然是一个 Android 新手。我正在尝试创建一个寻呼机应用程序。 ReceiveAlert 活动显示警报的内容并在打开时播放声音。经过一番挣扎和谷歌搜索,我已经将音量调到最大以播放声音。这是 onCreate 代码:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_receive_alert);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        checkPermission();
    }

    Bundle bundle = getIntent().getExtras();

    if (bundle != null) {
        taskingalert = bundle.getString(SmsReceiver.EXTRA_MESSAGE);
        TextView receivedAlert = findViewById(R.id.receivedAlert);
        receivedAlert.setText(taskingalert);


        MediaPlayer player = MediaPlayer.create(getApplicationContext(), R.raw.thechain);
        final AudioManager mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);            final int originalVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume,AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
        player.setLooping(true);
        player.start();
    }
}

然后有一个按钮可以停止声音并移动到 SendResponse 活动。这是我遇到问题的地方。我的那部分代码是:

    public void respond(View view) {
    mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, originalVolume,AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
    player.release();
    player.stop();
    Intent intent = new Intent(this, SendResponseActivity.class);
    intent.putExtra(EXTRA_MESSAGE, taskingalert);
    startActivity(intent);
}

但是,Android Studio 将 mAudioManager、originalVolume 和 player 列为问题,我无法理解建议的解决方案。我认为它们都有一个仅限于 onCreate 的范围,并且不适用于“响应”。

我尝试将整个 public void respond(View view) 放在 onCreate 中,但这无济于事,还会引发其他问题。

我尝试在 onCreate 之前声明 MediaPlayer 播放器、AudioManager mAudioManager 和 originalVolume。这消除了 Android Studio 中的红色突出显示,但应用在测试中失败。

任何帮助将不胜感激。

【问题讨论】:

    标签: android android-studio


    【解决方案1】:

    你是对的。变量确实有范围。在这种情况下,您希望将mAudioManagerplayeroriginalVolume 声明为成员变量。现在它们只是局部变量,只存在于onCreate 方法中。

    请参阅此文档以供参考:https://docs.oracle.com/javase/tutorial/java/javaOO/variables.html

    在您的情况下,从 onCreate 方法中提取变量声明就足够了,如下所示:

    AudioManager mAudioManager;
    MediaPlayer player;
    int originalVolume
    
        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive_alert);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            checkPermission();
        }
    
        Bundle bundle = getIntent().getExtras();
    
        if (bundle != null) {
            taskingalert = bundle.getString(SmsReceiver.EXTRA_MESSAGE);
            TextView receivedAlert = findViewById(R.id.receivedAlert);
            receivedAlert.setText(taskingalert);
    
    
            player = MediaPlayer.create(getApplicationContext(), R.raw.thechain);
            mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
            originalVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
            int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
            mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume,AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
            player.setLooping(true);
            player.start();
        }
    }
    

    三件事发生了变化:

    • 变量在顶部声明其类型
    • onCreate 内,变量被赋值(注意:我们没有在此处指定变量类型。这仅在声明时需要。)
    • 变量不能再是最终的,因为先声明并稍后获取值

    【讨论】:

    • 解决了。谢谢亚历山大。对于之后来到这里的任何人,我的第二段代码中也有一个错误,这可能会混淆我对问题的理解。 player.stop 必须在 player.release 之前。
    猜你喜欢
    • 1970-01-01
    • 2012-11-10
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多