码迷,mamicode.com
首页 > 其他好文 > 详细

TelephonyManager对黑名单的管理

时间:2016-06-22 18:55:52      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.provider.ContactsContract;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;

public class BlockMain extends Activity {
  //记录黑名单的List
  ArrayList<String> blockList = new ArrayList<String>();
  TelephonyManager tManager;
  //监听通话状态的监听器
  CustomPhoneCallListener cpListener;
  public class CustomPhoneCallListener extends PhoneStateListener{
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
      switch (state) {
        case TelephonyManager.CALL_STATE_IDLE:
          break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
          break;
        //电话呼入时
        case TelephonyManager.CALL_STATE_RINGING:
        //如果该号码属于黑名单
        if(isBlock(incomingNumber)){
          try {
            Method method = Class.forName("android.os.ServiceManager")
                          .getMethod("getService", String.class);
            //获取远程TELEPHONY_SERVICE的IBinder对象的代理
            IBinder binder = (IBinder) method.invoke(null,
                    new Object[]{TELEPHONY_SERVICE});
            //将IBinder对象的代理转换为ITelephony对象
            ITelephony telephony = ITelephony.Stub.asInterface(binder);
            //挂断电话
            telephony.endCall();
          } catch (NoSuchMethodException e) {
            e.printStackTrace();
          } catch (ClassNotFoundException e) {
            e.printStackTrace();
          } catch (IllegalArgumentException e) {
            e.printStackTrace();
          } catch (IllegalAccessException e) {
            e.printStackTrace();
          } catch (InvocationTargetException e) {
            e.printStackTrace();
          } catch (RemoteException e) {
            e.printStackTrace();
          }
        }

        default:
          break;
      }
      super.onCallStateChanged(state, incomingNumber);
    }
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_block_main);
    //获取系统的TelephonyManager管理器
    tManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    cpListener = new CustomPhoneCallListener();
    //通过TelephonyManager监听通话状态的改变
    tManager.listen(cpListener, PhoneStateListener.LISTEN_CALL_STATE);
    //获取程序的按钮,并为它的单击事件绑定监听器
    findViewById(R.id.managerBlock).setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        // 查询联系人的电话号码
        final Cursor cursor = getContentResolver().query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                      null, null, null, null);
        BaseAdapter adapter = new BaseAdapter() {

          @Override
          public View getView(int position, View convertView, ViewGroup parent) {
            cursor.moveToPosition(position);
            CheckBox rb = new CheckBox(BlockMain.this);
            //获取联系人的电话号码,并去掉中间的中划线
            String number = cursor.getString(cursor
                .getColumnIndex(ContactsContract.
                CommonDataKinds.Phone.NUMBER)).replace("-", "");
            rb.setText(number);
            //如果该号码已经被加入黑名单,默认勾选该号码
            if(isBlock(number)){
            rb.setChecked(true);
          }
          return rb;
        }

        @Override
        public long getItemId(int position) {
          return position;
        }

        @Override
        public Object getItem(int position) {
          return position;
        }

        @Override
        public int getCount() {
          return cursor.getCount();
        }
      };
      //加载list.xml布局文件对象的View
      View selectView = getLayoutInflater().inflate(R.layout.list, null);
      //获取selectView中的名为list的ListView组件
      final ListView listView = (ListView) selectView.findViewById(R.id.list);
      listView.setAdapter(adapter);
      new AlertDialog.Builder(BlockMain.this).setView(selectView)
          .setPositiveButton("确定", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
          // 清空blockList集合
          blockList.clear();
          //遍历ListView组件的每个列表项
          for(int i = 0; i<listView.getCount() ; i++){
            CheckBox checkBox = (CheckBox) listView.getChildAt(i);
            //如果该列表项被勾选
            if(checkBox.isChecked()){
            //添加该列表项的电话号码
            blockList.add(checkBox.getText().toString());
          }
        }
        System.out.println(blockList);
      }
     }).show();
    }
  });
  }

  //判断某个电话号码是否在黑名单中
  public boolean isBlock(String phone){
    for(String s1 : blockList){
      if(s1.equals(phone)){
        return true;
      }
    }
    return false;
  }

}

TelephonyManager对黑名单的管理

标签:

原文地址:http://www.cnblogs.com/jiww/p/5608089.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!