对应拨打电话只需要调用ACTION_CALL加对应电话号码即可如下:
void callPhone(){ Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:110")); //不能调用呼叫 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); }
答案是可以的,但是前提app必须用系统级别权限,及system/app/目录下方可。
首先确保应用具有如下权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.CALL_PHONE" /> <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/> <uses-permission android:name="android.permission.CALL_PRIVILEGED" />
void callPhone(String num){ Intent intent = new Intent("android.intent.action.CALL_PRIVILEGED",Uri.parse("tel:"+num)); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); }
开启拨通电话后设置开启扬声器功能:
首先,获取音频管理器:
audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
设置电话监听:
private class TelListner extends PhoneStateListener{ boolean comingPhone=false; @Override public void onCallStateChanged(int state, String incomingNumber) { switch (state) { case TelephonyManager.CALL_STATE_IDLE:/* 无任何状态 */ Log.d(tag, "phone idle"); if(this.comingPhone){ this.comingPhone=false; backMyApp(); setSpeekModle(false); } break; case TelephonyManager.CALL_STATE_OFFHOOK:/* 接起电话 */ Log.d(tag, "phone answer"); this.comingPhone=true; setSpeekModle(true); break; case TelephonyManager.CALL_STATE_RINGING:/* 电话进来 */ Log.d(tag, "phone coming"); this.comingPhone=true; setSpeekModle(true); break; } } }
TelephonyManager telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); TelListner listener = new TelListner() ; telManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
void setSpeekModle(boolean open){ //audioManager.setMode(AudioManager.ROUTE_SPEAKER); currVolume = audioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL); audioManager.setMode(AudioManager.MODE_IN_CALL); if(!audioManager.isSpeakerphoneOn()&&true==open) { audioManager.setSpeakerphoneOn(true); audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL), AudioManager.STREAM_VOICE_CALL); }else if(audioManager.isSpeakerphoneOn()&&false==open){ audioManager.setSpeakerphoneOn(false); audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL,currVolume, AudioManager.STREAM_VOICE_CALL); } }
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />