拦截来电和回复短信
lywdx
posted @ 2012年4月14日 23:12
in HelloAndroid
, 3962 阅读
由于系统API并没有提供Itelephony这个电话管理服务类的接口使用,所以就得通过源码中的Itelephony.aidl来生成电话管理服务接口,这样就能够使用到系统操作电话的功能。
新建包com.android.internal.telephony,在包内新建Itelephony.aidl,内容为
interface ITelephony{ boolean endCall(); void answerRingingCall(); /** * Allow mobile data connections. */ boolean enableDataConnectivity(); /** * Disallow mobile data connections. */ boolean disableDataConnectivity(); /** * Report whether data connectivity is possible. */ boolean isDataConnectivityPossible(); }
程序核心代码:
/* *从私有方法中获取ITelephony **/ private void phoner() { manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); Class<TelephonyManager> c = TelephonyManager.class; Method getITelephonyMethod = null; try { getITelephonyMethod = c.getDeclaredMethod("getITelephony", (Class[]) null); getITelephonyMethod.setAccessible(true); iTelephony = (ITelephony) getITelephonyMethod.invoke(manager, (Object[]) null); } catch (Exception e) { e.printStackTrace(); } }
/* *监听电话状态,判断是否是来电中 */ public void startService(){ //监听电话状态 PhoneStateListener phoneListener = new PhoneStateListener(){ @Override public void onCallStateChanged(int state,String incoming){ if(state == TelephonyManager.CALL_STATE_RINGING){ //判断电话状态为来电 phoner(); iTelephony.endCall();//拦截 sendSMS(); //发送信息 } } }; telephonyManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE); smsManager = SmsManager.getDefault();//获取系统默认的短信管理器 }
/* *根据来电号码回复内容 **/ public void sendSMS(String incoming) { incomingNumber = incoming; if (numberList.contains(incomingNumber)) { String smsText = editTextVisib.getText().toString();//获取要回复的短信内容 smsManager.sendTextMessage(incomingNumber, null, smsText, null,null); //incomingNumber目标号码,smsText短信内容 } }
2023年1月14日 10:07
There are a few ways to block incoming calls and reply to text messages. One way is to use the Do Not Disturb feature on your phone. This will block all calls and notifications unless you have whitelisted certain contacts. You can also use a third-party app to block calls and texts. Many of these apps allow you to set up auto-responses to texts, so you can let people know CBD hemp oil benefits you're busy without having to type out a response yourself.