每当从Android设备拨打电话时,我都会尝试打开扬声器。以下是我用过的代码。:
MainActivity:
CallReceiver call = new CallReceiver();
IntentFilter intentFilter = new IntentFilter(
Intent.ACTION_NEW_OUTGOING_CALL);
Main1Activity.this.registerReceiver(call, intentFilter);
呼叫接收方:
public class CallReceiver extends BroadcastReceiver {
AudioManager am;
@Override
public void onReceive(Context context, Intent intent) {
String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(context, phoneNumber, Toast.LENGTH_SHORT).show();
am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
boolean check = am.isSpeakerphoneOn();
if (check) {
Toast.makeText(context, "LoudSpeaker On: " + "Anchit",
Toast.LENGTH_LONG).show();
} else {
am.setMode(AudioManager.MODE_NORMAL);
am.setSpeakerphoneOn(true);
}
}
}
清单中的权限:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
我没有收到任何错误,但是我无法使扬声器正常工作。
请帮忙。