您的位置:首页 > 移动开发 > Android开发

Android之来电秀实现(二)

2016-03-11 16:53 155 查看
今天要说的主题是来电秀实现里面的电话接听与挂机,在这里我们需要用到ITelephony.aidl相关的一些文件,

如下图:



将如上文件导入到工程,去掉一些不需要的aidl关联,其中PhoneUtils是自己创建的,里面主要写的是调用接听和挂机函数。

public static com.android.internal.telephony.ITelephony getITelephony() throws Exception {
Class clazz = Class.forName("android.os.ServiceManager");
Method getServiceMethod = clazz.getMethod("getService", String.class);
IBinder iBinder = (IBinder) getServiceMethod.invoke(null, Context.TELEPHONY_SERVICE);
ITelephony iTelephony = ITelephony.Stub.asInterface(iBinder);
return ITelephony.Stub.asInterface(iBinder);
}


得到Itelphony后,里面有很多函数可以调用,下面说两个今天的主题,电话的接听和挂机,代码如下:

/**
* 接听电话
* @param context
*/
public static void answerRingingCall(Context context) {
try {
if(VERSION.SDK_INT > VERSION_CODES.GINGERBREAD){// android 2.3以上
try{
Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);
buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
context.sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED");
}catch(Exception e){
Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);
buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
context.sendOrderedBroadcast(buttonUp,null);
}
}else{
getITelephony().answerRingingCall();
}
} catch (Exception e) {
LogUtils.e(e);
}
}

/**
* 挂机
*/
public static void endRingingCall() {
try{
getITelephony().endCall();
}catch(Exception e){
LogUtils.e(e);
}
}


在这里要说明一点,因为andoid版本众多,有部分兼容性问题,其中自Android 5.0以后 ,接听电话answerRingingCall就已经失效了,通过模拟耳机按键接听电话也不起作用了,5.0以后接听电话暂时没有更好的处理方式,对于上面的接听实现,5.0以前上面的接听函数还是正常起作用的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: