如何在Android上用扬声器打电话

时间:2015-11-26 10:30:35

标签: android

我正在尝试与扬声器打电话,所以首先我尝试了这些代码:

audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE); 
final Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setSpeakerphoneOn(true);
}
}, 3000);
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + "09383551347"));
startActivity(callIntent);

但它不起作用,我也尝试了那些没有audioManager.setMode(AudioManager.MODE_IN_CALL);的代码,但仍然无法正常工作,我也尝试使用这些代码从BroadcastReceiver那样做:

public class CallDurationReceiver extends BroadcastReceiver {

    static boolean flag = false;
    static long start_time, end_time,total_time;
    static int available;
    static Handler mHandler;
    static int REQUEST_CODE = 0;
    static Context ff;

    @Override
    public void onReceive(final Context c, Intent intent) {
        ff = c;
        String action = intent.getAction();
        if (action.equalsIgnoreCase("android.intent.action.PHONE_STATE")) {
            if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
            TelephonyManager.EXTRA_STATE_OFFHOOK)) {
                start_time = System.currentTimeMillis();
audioManager = (AudioManager) ff.getSystemService(Context.AUDIO_SERVICE); 
final Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setSpeakerphoneOn(true);
}
}, 3000);
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + "09383551347"));
startActivity(callIntent);
            }
            if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
            TelephonyManager.EXTRA_STATE_IDLE)) {
                end_time = System.currentTimeMillis();
                total_time = end_time - start_time;

            }
        } 
    }
}

但仍然没有用,所以任何解决方案?

0 个答案:

没有答案