Android黑名单自动挂断电话

第一种黑名单挂断电话

使用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.java

package 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>




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值