您的位置:首页 > 其它

安卓移动软件开发:手机防火墙的实现

2018-06-17 06:06 344 查看

一、需求分析
不少人平常都会收到骚扰电话,在目前大数据时代,个人隐私无法得到完全的保障,因此,非常需要一个简易的黑名单功能,对骚扰电话进行拦截。

二、功能描述
用户将一些手机号加入黑名单后,如果有电话拨入,判断手机号码是否匹配,如果相同则进行挂断。

三、过程分析

启动服务后,用户将骚扰号码存入黑名单中,软件监测电话状态,当有电话拨入后,判断是否存在于黑名单中,若匹配,则进行挂断;若不匹配,则保持原有状态。

四、预设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

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐