运行效果图
布局文件activity_main.xml
<?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="bzu.edu.cn.interceptcall.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入拦截号码"
android:gravity="center"
android:id="@+id/ed_ipnumber"/>
<Button
android:text="保存拦截号码"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="92dp"
android:id="@+id/btnintercept"
android:onClick="intercept"
android:background="#e6e6fa"/>
</RelativeLayout>
创建广播接收者OutcallReceiver.java继承BroadcastReceiver
package bzu.edu.cn.interceptcall;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
public class OutcallRecevier extends BroadcastReceiver {
public OutcallRecevier() {
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
String outcallnumber=getResultData();
SharedPreferences sp=context.getSharedPreferences("config",Context.MODE_PRIVATE);
String number=sp.getString("number","");
if(outcallnumber.equals(number)){
setResultData(null);
}
}
}
界面交互代码MainActivity
package bzu.edu.cn.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 ed_ipnmber;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed_ipnmber= (EditText) findViewById(R.id.ed_ipnumber);
sp=getSharedPreferences("config",MODE_PRIVATE);
}
public void intercept(View view){
String number=ed_ipnmber.getText().toString().trim();
SharedPreferences.Editor editor=sp.edit();
editor.putString("number",number);
editor.commit();
Toast.makeText(MainActivity.this,"保存成功",Toast.LENGTH_LONG).show();
}
}
因为权限信息外拨电话的广播侵犯到了用户的隐私信息,因此需要在清单文件配置权限信息:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission>
广播接收者的创建与注册:
1、创建广播接收者
要对监听到的广播事件进行处理,需要创建一个类继承BroadcastReceiver,重写onReceive()方法,
public class MyBroadcastReceiver extends BroadcastReceiver{
public void onReceiver(Context context,Intent intent){
}
}
当监听到有广播发出时,就会调用onReceive()方法,在onReceive()中进行对事件的处理。
2、注册常驻型广播
常驻型广播是当应用程序关闭后,如果接收到其他应用程序发出的广播,那么该程序会自动重新启动。常驻型广播需要在清单文件中注册,具体代码如下所示:
<receiver
android:name=".OutcallRecevier"
android:enabled="true"//用于选择广播接收者是否可以由系统实例化
android:exported="true">//用于选择是否接收当前程序之外的广播
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"></action>
</intent-filter>
</receiver>
3、注册非常驻型广播
非常驻型广播依赖于注册广播的组件的生命周期,例如,在Activity中注册广播接收者,当Activity销毁后广播也随之被移除。这种广播事件在代码中注册,具体代码如下:
protected void onCreate(Bundle savedInstanceState){
super.onCreate(saveInstaceState);
MyBroadcastReceiver receiver=new MyBroadcastReceiver();
//实例化过滤器并设置要过滤的广播
String action="android.provider.Telephony.SMS_RECEIVED";
IntentFilter intentFilter=new IntentFilter(action);
//注册广播
registerReceiver(receiver,intentFilter);
}
protected void onDestroy(){//移除广播
super.onDestroy();
unregisterReceiver(receiver);
}