Android中电话拦截器案例

xiaoxiao2021-02-28  57

实验要求:实现拦截保存到手机中的号码

1.在main-activity布局文件中代码如下:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:background="@drawable/sdz" tools:context="cn.edu.bzu.interceptcall.MainActivity"> <EditText android:id="@+id/et_ipnumber" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入拦截号码"/> <Button android:text="保存拦截号码" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="82dp" android:onClick="click" android:id="@+id/button" /> </RelativeLayout> 2.在MainActivity中编写点击保存拦截号码按钮时的事件处理,代码如下:

package cn.edu.bzu.interceptcall; import android.content.Intent; import android.content.SharedPreferences; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private EditText et_ipnumber; private SharedPreferences sp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_ipnumber= (EditText) findViewById(R.id.et_ipnumber); //创建SharedPreferences对象 sp=getSharedPreferences("config",MODE_PRIVATE); } public void click(View view){ //获取用户输入的拦截号码 String number=et_ipnumber.getText().toString().trim(); //创建Editor对象,保存用户输入的拦截号码 SharedPreferences.Editor editor=sp.edit(); editor.putString("number",number); editor.commit(); Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show(); } } 3.创建广播接收者OutcallReceiver,代码如下:

package cn.edu.bzu.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对象,获取拦截号码 SharedPreferences sp=context.getSharedPreferences("config",Context.MODE_PRIVATE); String number =sp.getString("number",""); //判断是否是拦截号码 if (outcallnumber.equals(number)){ //清除电话 setResultData(null); } } } 4.注册广播接收者,在配置文件AndroidManifest中代码如下:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.edu.bzu.interceptcall"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" 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" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.intent.action.NEW_OUTGOING_CALL"/> </intent-filter> </receiver> </application> <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission> </manifest>5.运行效果图:

转载请注明原文地址: https://www.6miu.com/read-57884.html

最新回复(0)