拦截史迪仔电话(广播接收者)
- 创建程序:
创建一个名为InterceptCall 的应用程序,指定包名为cn.itcast.interceptcall,设计用户交互界面。
先把界面写好:(还有先准备一张图片 作为背景图,放在drawable 文件夹里面android:background="@drawable/sdz")
<?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:padding="15dp"
android:background="@drawable/sdz"
tools:context="com.example.asus.interceptcall.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"
android:paddingRight="5dp"
android:text="保存拦截号码"
android:textSize="16sp"/>
</RelativeLayout>
- 编写界面交互代码:
在MainActivity中编写的界面交互的代码,将要拦截的号码保存到SharedPreferences对象中
package com.example.asus.interceptcall;
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();
}
}
- 监听广播事件
创建一个广播接收者OutCallReceiver,用于接受外拨电话的广播
package com.example.asus.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) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
//获取拨打的电话号码
String outcallnumber=getResultData();
//创建sharedPreferces 对象,获取拦截号码
SharedPreferences sp=context.getSharedPreferences("config",Context.MODE_PRIVATE);
String number=sp.getString("number","");
//判断是否拦截号码
if(outcallnumber.equals(number)){
//清除电话
setResultData(null);
}
}
}
当我们输入完电话号码的时,Android系统会发送一个广播给电话拨号器的广播接收者,因此,想要拦截生效,就需要在Android Manifest.xml文件中注册广播接收者,来拦截外拨电话,由于涉及权限,所以还需要添加相应的权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.asus.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"></receiver>
<!--注册广播接收者-->
<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>
-
运行程序
-
测试拦截