本次要完成的任务是电话拦截,运行效果图如下:
1.首先在目录app/res/drawable中粘贴图片sdz.png作为背景图。
2.创建布局文件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:background="@drawable/sdz"
android:padding="15dp"
tools:context="com.example.administrator.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>
3.编写界面交互代码(MainActivity.Java),代码如下:
package com.example.administrator.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();
}
}
4.创建广播接收者,具体步骤如下图:
广播接收者OutCallReceiver代码如下:
package com.example.administrator.interceptcall;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
public class OutCallReceiver extends BroadcastReceiver {
public OutCallReceiver() {
}
@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对象,获取拦截号码
SharedPreferences sp=context.getSharedPreferences("config",Context.MODE_PRIVATE);
String number=sp.getString("number","");
//判断是否是拦截电话号码
if (outcallnumber.equals(number)){
//清除电话
setResultData(null);
}
}
}
5.配置清单文件AndroidManifest.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.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_CALLS"/>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RROCESS_OUTGOING_CALLS"/>
</manifest>
运行成功后,如下图所示:
我的计算机装的是Android7.0版本模拟器,所以拦截不到,该任务运行所需版本为Android6.0以下模拟器才可以正常拦截,版本升级会考虑上下兼容,但是在低版本能运行,在高版本未必能运行,所以要引入运行权限。之后便能成功运行。