无法在Android中解析Java中的符号工具包

问题描述 投票:0回答:1

我只希望一些简单的代码在Android的Java中使用时发出提示音,但是当我尝试在Android Studio中导入Toolkit类时,它说它无法解析该符号。为什么会这样,我该如何解决?如果我不能使用它,还有其他可以使用的简短而又简单的东西吗?

import java.awt.Toolkit; <---cannot resolve symbol Toolkit

Toolkit.getDefaultToolkit().beep(); <---cannot resolve symbol Toolkit
java audio toolkit beep
1个回答
0
投票

java.awt。*在Android上不起作用。但是您可以使用ToneGenerator

try {
    if (tGen == null) {
        tGen = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);
    }         
    tGen.startTone(ToneGenerator.TONE_PROP_BEEP2, 150);

    Handler handler = new Handler(Looper.getMainLooper());
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            if (tGen != null) {
                tGen.release();
                tGen = null;
            }
        }
    }, 150);
} catch (Exception e) {
    android.util.Log.d(TAG, "Couldn't play sound:" + e.getMessage());
}

[TONE_PROP_BEEP2以外还有其他音调,音量为100,持续时间为150,以毫秒为单位。

© www.soinside.com 2019 - 2024. All rights reserved.