Android黑名单电话挂断(aidl)

1,首先把Android电话监的aidl放入项目中
这里写图片描述

2.布局文件

<?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:orientation="vertical"
    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"
    tools:context="com.example.android20_phonestate.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        >
        <EditText
            android:layout_width="230dp"
            android:layout_height="wrap_content"
            android:id="@+id/et_sms_number"
            android:hint="请输入号码"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="查看联系人"
            android:id="@+id/btn_main_tel"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <Button
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:hint="设置黑名单"
            android:onClick="settings"
            />
        <Button
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:hint="取消黑名单"
            android:layout_marginLeft="10dp"
            android:onClick="qu"
            />
    </LinearLayout>
</LinearLayout>

3.MainActivity

public class MainActivity extends AppCompatActivity {

    private EditText et_sms_number;
    private Button btn_main_tel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et_sms_number = (EditText) findViewById(R.id.et_sms_number);

        btn_main_tel = (Button) findViewById(R.id.btn_main_tel);
        String number = PhoneUtil.getString(MainActivity.this, "number", "");
        et_sms_number.setText(number);

        btn_main_tel.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                MainActivity.this.startActivityForResult(intent, 1);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    Uri contactData = data.getData();
                    Cursor cursor = managedQuery(contactData, null, null, null,
                            null);
                    cursor.moveToFirst();
                    String nums = this.getContactPhone(cursor);
                    Log.i("test", "所选手机号为:" + nums);
                    //将编辑器直接赋值
                    et_sms_number.setText(nums);
                }
                break;
            default:
                break;
        }
    }

    private String getContactPhone(Cursor cursor) {
        // TODO Auto-generated method stub
        int phoneColumn = cursor
                .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
        int phoneNum = cursor.getInt(phoneColumn);
        String result = "";
        if (phoneNum > 0) {
            // 获得联系人的ID号
            int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
            String contactId = cursor.getString(idColumn);
            // 获得联系人电话的cursor
            Cursor phone = getContentResolver().query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "="
                            + contactId, null, null);
            if (phone.moveToFirst()) {
                for (; !phone.isAfterLast(); phone.moveToNext()) {
                    int index = phone
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                    int typeindex = phone
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
                    int phone_type = phone.getInt(typeindex);
                    String phoneNumber = phone.getString(index);
                    result = phoneNumber;
                }
                if (!phone.isClosed()) {
                    phone.close();
                }
            }
        }
        return result;
    }


    public void settings(View view) {

        //拿到收入框的号码
        final String number = et_sms_number.getText().toString();
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("提示信息");
        builder.setIcon(R.drawable.star);
        builder.setMessage("你确定将"+number+"设为黑名单吗?");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                PhoneUtil.setString(MainActivity.this, "number", number);
                Toast.makeText(MainActivity.this, "设置黑名单成功", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消",null);
        builder.show();
    }


    public void qu(View view){
        String number = PhoneUtil.getString(MainActivity.this, "number", "");

        if (!"".equals(number)) {
            PhoneUtil.setString(MainActivity.this, "number", "");
            Toast.makeText(this, "取消成功", Toast.LENGTH_SHORT).show();
        }
    }
}

4.存号码

public class PhoneUtil {
    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);
    }
}

5.获取电话的号码

public class MyPhoneState extends BroadcastReceiver {

    private TelephonyManager tm;

    @Override
    public void onReceive(Context context, Intent intent) {
        if("android.intent.action.PHONE_STATE".equals(intent.getAction())){
            //获取电话号码
            String number=intent.getStringExtra("incoming_number");
            Log.i("test","主人来电话啦,啦啦啦啦"+number);
            Toast.makeText(context, "电话进来了:"+number, Toast.LENGTH_SHORT).show();

            //获取电话状态
            //电话管理者
            tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            int state= tm.getCallState();
            switch (state) {
                case TelephonyManager.CALL_STATE_RINGING:
                    //得到电话号码
                    Log.i("test","来电了"+number);
                    //得到电话管理者类对象

                    Class<TelephonyManager> clazz=TelephonyManager.class;
                    //得到方法
                    Method method= null;
                    try {
                        method = clazz.getDeclaredMethod("getITelephony",null);
                    } catch (NoSuchMethodException e) {
                        e.printStackTrace();
                    }
                    //设置可访问
                    method.setAccessible(true);
                    //执行方法
                    ITelephony iTelephony= null;
                    try {
                        iTelephony = (ITelephony) method.invoke(tm,null);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                    // /判断
                    String n = PhoneUtil.getString(context, "number", "").trim();
                    if(n.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;
            }
        }
    }
}

6.清单文件配置

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android20_phonestate">


    <!--读取电话的状态权限-->
    <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
    <!-- 读取联系人 -->
    <uses-permission android:name="android.permission.READ_CONTACTS"></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=".MainActivity">
            <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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值