即使我的应用已关闭,我也希望在后台捕捉音量级别。我正在使用AudioManager。但我得到以下错误: ContentObserver中的ContentObserver(android.os.Handler)无法应用于(java.util.logging.Handler) 私人ContentObserver mVolumeObserver =新ContentObserver( mHandler ){
我按照下面的链接来解决我的问题: any way to detect volume key presses or volume changes with android service?
公共类BackGroundServices扩展了服务{
AudioManager mAudioManager;
Handler mHandler;
private ContentObserver mVolumeObserver = new ContentObserver(mHandler) {
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
if(mAudioManager != null){
final int volume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
Log.e("TEST","VOLUME STREAM IS : " + volume);
}
}
};
@Override
public IBinder onBind(Intent arg0){
return null;
}
@Override
public void onCreate(){
super.onCreate();
mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
Uri uri = Settings.System.getUriFor(Settings.System.VOLUME_SETTINGS[AudioManager.STREAM_RING]);
getContentResolver().registerContentObserver(uri, true, mVolumeObserver);
}
@Override
public int onStartCommand(Intent intent, int flag, int startId) {
Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
return START_STICKY;
}
@Override
public void onDestroy () {
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();
getContentResolver().unregisterContentObserver(mVolumeObserver);
}
}
答案 0 :(得分:0)
您使用了错误的导入。您正在使用Android的处理程序,但您使用的导入是java.util.logging.handler。该错误还清楚地说明了预期的进口。所以试试android.os.handler并且错误应该消失。