【手机勤务员】android自动切断来电,如果是手机号码回复一条短信
当你的手机需要更换新号码的时候,或者当你出国旅行不需要携带国内手机的时候,你的手机可以放在家中,当有人来电时自动挂断,如果对方是手机号码(11位长首位为1)就回复一条短信。
废话少提,直接上代码!
1、必须有如下权限:
<uses-permission android:name="android.permission.SEND_SMS" /> <uses-permission android:name="android.permission.CALL_PHONE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" />
2、在工程的Package Explorer中的src按右键选New、Package,输入:com.android.internal.telephony
3、在com.android.internal.telephony按右键选New、File,文件名:ITelephony.aidl,输入的内容如下:
package com.android.internal.telephony; interface ITelephony { boolean endCall(); }
4、在MainActivity加入以下代码:
private ITelephony iTelephony = null; private TelephonyManager iTeleManager = null; public void initTelephony() { iTeleManager = (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(iTeleManager, (Object[])null); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } PhoneStateListener psl = new PhoneStateListener() { @Override public void onCallStateChanged(int state, String incomingNumber) { // TODO Auto-generated method stub super.onCallStateChanged(state, incomingNumber); switch(state){ case TelephonyManager.CALL_STATE_RINGING: Log.i("DEBUG", "uncall : " + incomingNumber); Message msg = new Message(); msg.what = 3618; msg.obj = incomingNumber; handler.sendMessage(msg); try { // 当电话接入时,自动挂断。 iTelephony.endCall(); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initTelephony(); iTeleManager.listen(psl, PhoneStateListener.LISTEN_CALL_STATE); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); iTeleManager.listen(null, 0); } private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); myHandleMessage(msg); } }; private void myHandleMessage(Message msg) { if(msg.what == 3618) { String phoneNumber = (String)msg.obj; if(phoneNumber.length() == 11 && phoneNumber.charAt(0) == '1') { SmsManager sms = SmsManager.getDefault(); String smsmsg = "您好,机主已经更新号码,有事请拨打:188 2318 3718。【手机勤务员】竭诚为您提供优质服务!"; sms.sendTextMessage(phoneNumber, null, smsmsg, null, null); } } }
至此,手机勤务员的自动挂机并短信通知的优质服务宣告完成!
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。