1,首先把Android电话监的aidl放入项目中
2,activity_phonestate.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_phonestate"
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:orientation="vertical"
tools:context="com.zking.hgz_android_broadcastreceiver.Phonestate">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入黑名单号码"
android:id="@+id/et_main"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置黑名单"
android:onClick="set"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消黑名单"
android:onClick="qu"/>
</LinearLayout>
</LinearLayout>
3,Phonestate
package com.zking.hgz_android_broadcastreceiver;
import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telecom.PhoneAccount;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class Phonestate extends AppCompatActivity {
private EditText et_main;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_phonestate);
et_main = (EditText) findViewById(R.id.et_main);
String number=PrefUtil.getString(Phonestate.this,"number","");
et_main.setText(number);
}
public void set(View view){
// //得到号码
final String number=et_main.getText().toString();
AlertDialog.Builder builder=new AlertDialog.Builder(Phonestate.this);
builder.setTitle("温馨提示");
builder.setMessage("你确定设置"+number+"黑名单吗?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Phonestate.this, "黑名单设置成功", Toast.LENGTH_SHORT).show();
PrefUtil.setString(Phonestate.this,"number",number);
}
});
builder.setNegativeButton("取消",null);
builder.show();
}
public void qu(View view){
String number=PrefUtil.getString(Phonestate.this,"number","");
if(!"".equals(number)){
PrefUtil.setString(Phonestate.this,"number","");
Toast.makeText(this, "取消成功", Toast.LENGTH_SHORT).show();
}
}
}
4,MyPhonestate
package com.zking.hgz_android_broadcastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.RemoteException;
import android.telecom.TelecomManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
import com.android.internal.telephony.ITelephony;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Created by hgz on 2017/2/7.
*/
public class MyPhonestate extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if("android.intent.action.PHONE_STATE".equals(intent.getAction())){
//得到号码
String number=intent.getStringExtra("incoming_number");
Toast.makeText(context, "有电话进来了,号码是:"+number, Toast.LENGTH_SHORT).show();
//获取电话状态,电话管理者
TelephonyManager tm= (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int state=tm.getCallState();
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
Toast.makeText(context, "未接听", Toast.LENGTH_SHORT).show();
//得到电话管理者类对象
Class<TelephonyManager> clazz=TelephonyManager.class;
//得到方法
try {
Method method=clazz.getDeclaredMethod("getITelephony",null);
//设置可访问
method.setAccessible(true);
//执行方法
ITelephony iTelephony= (ITelephony) method.invoke(tm,null);
//判断
//得到号码
String n=PrefUtil.getString(context,"number","").trim();
if(n.equals(number)){
iTelephony.endCall();
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(context, "通话中", Toast.LENGTH_SHORT).show();
break;
case TelephonyManager.CALL_STATE_IDLE:
Toast.makeText(context, "已挂断", Toast.LENGTH_SHORT).show();
break;
}
}
}
}
5,PrefUtil
package com.zking.hgz_android_broadcastreceiver;
import android.content.Context;
import android.content.SharedPreferences;
/**
* Created by hgz on 2017/2/9.
*/
public class PrefUtil {
public static void setString(Context context,String key,String value){
SharedPreferences sp=context.getSharedPreferences("phone",Context.MODE_PRIVATE);
sp.edit().putString(key,value).commit();
}
public static String getString(Context context, String key, String defValue) {
SharedPreferences sharedPreferences = context.getSharedPreferences("phone", Context.MODE_PRIVATE);
return sharedPreferences.getString(key, defValue);
}
}
6,AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zking.hgz_android_broadcastreceiver">
<!--读取电话状态权限-->
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Phonestate">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--配置广播接受者-->
<receiver android:name=".MyPhonestate">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"></action>
</intent-filter>
</receiver>
</application>
</manifest>