第一種黑名單掛斷電話
使用AIDL文件自動生成接口,把NeighboringCellInfo.aidl和ITelephony.aidl放到對應的包中。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.zking.administrator.g160628_android24_telephone.MainActivity">
<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>
MainActivity.java
package com.zking.administrator.g160628_android24_telephone;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText et_main;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_main = (EditText) findViewById(R.id.et_main);
String number=PrefUtil.getString(MainActivity.this,"number","");
et_main.setText(number);
}
public void set(View view){
// //得到號碼
final String number=et_main.getText().toString();
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("溫馨提示");
builder.setMessage("你確定設置"+number+"黑名單嗎?");
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "黑名單設置成功", Toast.LENGTH_SHORT).show();
PrefUtil.setString(MainActivity.this,"number",number);
}
});
builder.setNegativeButton("取消",null);
builder.show();
}
public void qu(View view){
String number=PrefUtil.getString(MainActivity.this,"number","");
if(!"".equals(number)){
PrefUtil.setString(MainActivity.this,"number","");
Toast.makeText(this, "取消成功", Toast.LENGTH_SHORT).show();
}
}
}
MySMSReceiver.java
在來去電的廣播中,判斷電話狀態,如果是響鈴狀態,得到電話號碼,如果是要攔截的電話就執行下面的操作
package com.zking.administrator.g160628_android24_telephone;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.RemoteException;
import android.telephony.TelephonyManager;
import android.widget.Toast;
import com.android.internal.telephony.ITelephony;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Created by Administrator on 2017/7/15.
*/
public class MySMSReceiver extends BroadcastReceiver {
private String a;
@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> telephonyManagerClass=TelephonyManager.class;
//得到方法
try {
Method method=telephonyManagerClass.getDeclaredMethod("getITelephony",null);
//設置可訪問
method.setAccessible(true);
//執行方法
ITelephony iTelephony= (ITelephony) method.invoke(tm,null);
//判斷
//得到號碼
String n=PrefUtil.getString(context,"number","").trim();
if(n.equals(number)){
try {
iTelephony.endCall();
} catch (RemoteException e) {
e.printStackTrace();
}
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException 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;
}
}
}
}
PrefUtil.javapackage com.zking.administrator.g160628_android24_telephone;
import android.content.Context;
import android.content.SharedPreferences;
/**
* Created by Administrator on 2017/7/15.
*/
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);
}
}
自動掛斷的權限AndroidManifest.xml <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
第二種黑名單自動掛斷電話
MyPhone.java
在來去電的廣播中,判斷電話狀態,如果是響鈴狀態,得到電話號碼,如果是要攔截的電話就執行下面的操作
package com.zking.administrator.g160628_android24_telephone2;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.RemoteException;
import android.telephony.TelephonyManager;
import android.util.Log;
import com.android.internal.telephony.ITelephony;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Created by Administrator on 2017/7/16.
*/
public class MyPhone extends BroadcastReceiver{
private ITelephony iTelephony;
@Override
public void onReceive(Context context, Intent intent) {
if("android.intent.action.PHONE_STATE".equals(intent.getAction())){
//得到電話管理者
TelephonyManager telephonyManager= (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
//得到電話的狀態
int state=telephonyManager.getCallState();
switch (state){
case TelephonyManager.CALL_STATE_RINGING:
//得到電話號碼
String number=intent.getStringExtra("incoming_number");
Log.i("test","來電話了。。"+number);
//得到電話管理者類對象
Class<TelephonyManager> telephonyManagerClass=TelephonyManager.class;
//得到方法
Method method=null;
try {
method=telephonyManagerClass.getDeclaredMethod("getITelephony",null);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
//允許訪問私有的方法
method.setAccessible(true);
//執行方法
try {
iTelephony = (ITelephony) method.invoke(telephonyManager,null);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
//判斷是不是這個號碼,是的話就掛斷
if("18711185761".equals(number)){
try {
iTelephony.endCall();
} catch (RemoteException e) {
e.printStackTrace();
}
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i("test","通話中。。。");
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.i("test","掛斷");
break;
}
}
}
}
AndroidManifest.xml<!--權限-->
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<receiver android:name=".MyPhone">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"></action>
</intent-filter>
</receiver>