我想使用 ContentObserver 来处理麦克风状态。但它不起作用。onChange 方法中没有任何内容。
public class MainActivity extends Activity implements OnClickListener{
AudioManager amAudioManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
amAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
getContentResolver().registerContentObserver(
System.getUriFor(System.MUTE_STREAMS_AFFECTED),
false, mMuteObserver);
}
private ContentObserver mMuteObserver = new ContentObserver(new Handler()) {
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
amAudioManager.setMicrophoneMute(false);
}
};
@Override
public void onClick(View v) {
int id = v.getId();
//just for test functional
amAudioManager.setMicrophoneMute(true);
}
}
Android清单文件权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />**