Android 拦截电话 适用版本4.4
Android 拦截电话
1.创建一个名为InterceptCall的应用程序
(1)在activity_main.xml文件定义一个相对布局,在该布局中放置了两个控件(EditText 和 Button)用于输入和保存拦截号码,同时在Button控件中添加onClick属性为按钮添加单击事件。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <EditText android:id="@+id/et_ipnumber" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入拦截号码"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/et_ipnumber" android:layout_centerHorizontal="true" android:background="#ACD6FF" android:onClick="click" android:paddingLeft="5dp"w android:paddingRight="5dp" android:text="保存拦截号码" android:textSize="16sp" tools:ignore="OnClick" /> </RelativeLayout>
(2)在MainActivity 中编写界面交互的代码,初始化EditText对象和SharedPreferences对象,然后创建click()方法实现单击事件,当用户单击”保存拦截号码“按钮时,就会将要拦截的号码保存到SharedPreferences中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package com.example.interceptcall; import androidx.appcompat.app.AppCompatActivity; import android.content.SharedPreferences; import android.os.Bundle; import android.os.SharedMemory; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private EditText et_ipnimber; private SharedPreferences sp; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_ipnimber=(EditText) findViewById(R.id.et_ipnumber); sp = getSharedPreferences( "config" ,MODE_PRIVATE); } public void click(View view){ String number = et_ipnimber.getText().toString().trim(); SharedPreferences.Editor editor = sp.edit(); editor.putString( "number" ,number); editor.commit(); Toast.makeText( this , "保存成功" ,Toast.LENGTH_SHORT).show(); } } |
(3)创建一个监听广播事件
创建OutCallReceiver.java用于接受外拨电话的广播, 利用getResultDatea()方法可以获得外拨的电话号码, 获取被保存在SharedPreferences中的拦截号码,然后判断是否用户外拨电话号码与拦截号码一致,如果一致泽调用setResultData()方法,将其清除,系统自动关闭电话功能,如果不一致则继续通话。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package com.example.interceptcall; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; public class OutCallReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String outcallnumber =getResultData(); SharedPreferences sp=context.getSharedPreferences( "config" ,Context.MODE_PRIVATE); String number = sp.getString( "number" , "" ); if (outcallnumber.equals(number)){ setResultData( null ); } } } |
(4)当输入完电话号码拨打电话时Android 系统将发送一个广播(android.intent.action.NEW_OUTGOING_CALL)给电话拨号器的广播接收者,因此想要拦截生效,就需要在AndroidManifest.xml文件中注册一个广播接收者,用来拦截外拨电话的广播,因为外拨电话涉及到权限问题,所以我们也要在清单文件中添加相应的权限。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.interceptcall"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!--注册广播接收者--> <receiver android:name=".OutCallReceiver"> <intent-filter> <action android:name="android.intent.action.NEW_OUTGOING_CALL"/> </intent-filter> </receiver> </application> <!--设置权限--> <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/> </manifest>
已下为测试的效果截图
保存完拦截电话后返回主页
进入拨号界面 输入10086
点击拨号如果返回主界面即可成功
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!