安卓移动软件开发:手机防火墙的实现
一、需求分析
不少人平常都会收到骚扰电话,在目前大数据时代,个人隐私无法得到完全的保障,因此,非常需要一个简易的黑名单功能,对骚扰电话进行拦截。
二、功能描述
用户将一些手机号加入黑名单后,如果有电话拨入,判断手机号码是否匹配,如果相同则进行挂断。
三、过程分析
启动服务后,用户将骚扰号码存入黑名单中,软件监测电话状态,当有电话拨入后,判断是否存在于黑名单中,若匹配,则进行挂断;若不匹配,则保持原有状态。
四、预设API
TelephonyManager: 电话服务的管理器
context.getSystemService(Context.TELEPHONY_SERVICE): 得到它的对象
listen(phoneStateListener,PhoneStateListener.LISTEN_CALL_STATE): 监听电话状态
PhoneStateListener: 电话状态监听器
onCallStateChanged(int state, String incomingNumber): 状态改变的回调方法
TelephonyManager.CALL_STATE_IDLE : 空闲状态
TelephonyManager.CALL_STATE_RINGING : 拨入状态
TelephonyManager.CALL_STATE_OFFHOOK : 通话状态
另:因为安卓没有公开结束通话的API,如果需要结束通话,必须使用AIDL与电话管理服务进行通信,并调用服务中的API实现结束通话。
五、代码实现
1.ListenCallService
package com.example.appservice; import java.lang.reflect.Method; import com.android.internal.telephony.ITelephony; import android.app.Service; import android.content.Context; import android.content.Intent; import android.os.IBinder; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.util.Log; public class ListenCallService extends Service { private TelephonyManager tm; private PhoneStateListener listener = new PhoneStateListener() { // 当通话状态发生改变时调用 public void onCallStateChanged(int state, String incomingNumber) { switch (state) { case TelephonyManager.CALL_STATE_IDLE:// 空闲(挂断电话/未来电之前) Log.e("TAG", "空闲(挂断电话/未来电之前)"); break; case TelephonyManager.CALL_STATE_RINGING:// 响铃 Log.e("TAG", "响铃"); // 如果来电是黑名单号(110),就挂断电话 if ("110".equals(incomingNumber)) { try { endCall(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } break; case TelephonyManager.CALL_STATE_OFFHOOK:// 接通 Log.e("TAG", "接通"); break; default: break; } } }; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } /* * 挂断电话 */ private void endCall() throws Exception { // 通过反射调用隐藏的API // 得到隐藏类的Class对象 Class c = Class.forName("android.os.ServiceManager"); // 得到方法所对应的Method对象 Method method = c.getMethod("getService", String.class); // 调用方法 IBinder iBinder = (IBinder) method.invoke(null, Context.TELEPHONY_SERVICE); // 得到接口对象 ITelephony telephony = ITelephony.Stub.asInterface(iBinder); // 结束通话 telephony.endCall(); } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); // 得到电话管理器 tm = (TelephonyManager) this .getSystemService(Context.TELEPHONY_SERVICE); // 监听电话状态 tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); // 停止电话监听 tm.listen(listener, PhoneStateListener.LISTEN_NONE); } }
2.黑名单拦截功能的开关
<span style="white-space:pre"> </span>public void startListenCall(View v){ startService(new Intent(this, ListenCallService.class)); } public void stopListenCall(View v){ stopService(new Intent(this, ListenCallService.class)); }
3.注册Service
<service android:name="com.example.appservice.ListenCallService"></service>
4.设置开机即开启黑名单
<receiver android:name="com.example.appservice.BootReceiver"> <intent-filter > <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver>
package com.example.appservice; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; /** * 接收开机完成广播的receiver * @author Xiaocici * */ public class BootReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //启动电话监听的receiver context.startService(new Intent(context, ListenCallService.class)); } }
六、总结分析
软件可以初步满足需求,可着眼于设置免打扰时段、拒接回执短信等服务,因为本人能力有限,暂时只完成对骚扰电话的拦截,之后会近一步完善功能。
作者:陈语童
原文链接:https://blog.csdn.net/weixin_41402015/article/details/80716498
- 安卓与IOS通过hessian接口实现用友系统移动手机订单4安卓手机开发
- 安卓与IOS通过hessian接口实现用友系统移动手机订单5苹果手机开发
- 十大恶意软件让安卓手机成为噩梦,移动代码签名证书重要性大增
- 微信开发之移动手机WEB页面(HTML5)Javascript实现一键拨号及短信发送功能
- 安卓与IOS通过hessian接口实现用友系统移动手机订单2服务器端Servlet实现
- 安卓手机应用软件开发 手机软件开发基础
- 微信开发之移动手机WEB页面(HTML5)Javascript实现一键拨号及短信发送功能
- [HTML] 微信开发之移动手机WEB页面(HTML5)Javascript实现一键拨号及短信发送功能
- 手机移动电玩城游戏开发捕鱼软件开发华软源头开发公司
- 【android开发】手机应用管理器的实现之实现软件加锁(四)
- [Phonegap+Sencha Touch] 移动开发19 某些安卓手机上弹出消息框 点击后不消失的解决办法
- 安卓TV开发(三) 移动智能设备之实现主流TV电视盒子焦点可控UI
- 智能家居APP开发,智能穿戴APP开发,手机APP开发,IOS和安卓等软件项目开发
- 微信开发之移动手机WEB页面(HTML5)Javascript实现一键拨号及短信发送功能
- Android网络开发回顾之旅 ① 在Android手机集成使用MQTT协议 ,实现搞掂移动控制硬件端。(附带Demo)
- 《BREW进阶与精通——3G移动增值业务的运营、定制与开发》连载之37---BREW 在手机中的软件层次
- 微信开发之移动手机WEB页面(HTML5)Javascript实现一键拨号及短信发送功能
- 微信开发之移动手机WEB页面(HTML5)Javascript实现一键拨号及短信发送功能
- [置顶] 安卓手机软件开发
- 用mqtt实现安卓手机软件的服务器推送功能