设计一个电话拦截器,在文本框中输入拦截号码,点击保存拦截号码成功拦截号码
运行效果图如下:
代码如下:
AndroidManifest.xml:
<?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"/>
</manifest>
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="cn.edu.bzu.interceptcall.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_ipnumber"
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.java
package cn.edu.bzu.interceptcall;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
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);
sp=getSharedPreferences("config",MODE_PRIVATE);
}
public void click(View view){
String number=et_ipnumber.getText().toString().trim();
SharedPreferences.Editor editor=sp.edit();
editor.putString("number",number);
editor.commit();
Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();;}
}
OutcallReceiver.java: