文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Android编程中黑名单的实现方法

2022-06-06 09:00

关注

本文实例讲述了Android编程中黑名单的实现方法。分享给大家供大家参考,具体如下:

说明:由于挂断电话android   api不是对外开放的,所以需要使用反射的方法得到拨打电话的服务。

1.将android源代码中的"aidl"文件拷贝到项目中

这样项目中会生成两个包:android.telephony;此包中文件为:NeighboringCellInfo.aidl

com.android.internal.telephony;此包中文件为:ITelephony.aidl

2.通过反射挂断电话;代码如下:



public void endCall() {
    try {
      Method method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
      IBinder binder = (IBinder) method.invoke(null, new Object[]{TELEPHONY_SERVICE});
      ITelephony telephony = ITelephony.Stub.asInterface(binder);
      telephony.endCall();
    } catch (Exception e) {
      e.printStackTrace();
    }
}

3.删除通话记录中的记录



public void deleteCallLog(String incomingNumber) {
    ContentResolver resolver = getContentResolver();
    Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI, null,"number=?", new String[]{incomingNumber}, null);
    if(cursor.moveToNext()){
      String id = cursor.getString(cursor.getColumnIndex("_id"));
      resolver.delete(CallLog.Calls.CONTENT_URI, "_id=?", new String[]{id});
    }
}

4.直接这样调用是不能删除电话记录的,因为生成电话记录的过程是一个异步的过程,在挂断电话之后不能立即删除电话记录,所以这里要使用ContentObserver(内容观察者)


private class MyObserver extends ContentObserver{
    private String incomingNumber;
    public MyObserver(Handler handler,String incomingNumber) {
      super(handler);
      this.incomingNumber = incomingNumber;
    }
    @Override
    public void onChange(boolean selfChange) {
      super.onChange(selfChange);
      deleteCallLog(incomingNumber);
      getContentResolver().unregisterContentObserver(this);
    }
}

6.最后把整个service代码贴到下面


public class AddressService extends Service{
  private static final String TAG = "AddressService";
  private TelephonyManager manager;
  private MyPhoneListener listener;
  private WindowManager wManager;
  private View view;
  private SharedPreferences sp;
  long startTime = 0;
  long endTime = 0;
  private BlackNumberDao dao;
  @Override
  public IBinder onBind(Intent arg0) {
    return null;
  }
  
  @Override
  public void onCreate() {
    super.onCreate();
    listener = new MyPhoneListener();
    manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    wManager = (WindowManager) this.getSystemService(WINDOW_SERVICE);
    manager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
    sp = getSharedPreferences("config", MODE_PRIVATE);
    dao = new BlackNumberDao(this);
//   if(3000>(endTime - startTime)){
//     String ns = Context.NOTIFICATION_SERVICE;
//     NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
//     //定义通知栏展现的内容信息
//     int icon = R.drawable.icon5;
//     CharSequence tickerText = "我的通知栏";
//     long when = System.currentTimeMillis();
//     Notification notification = new Notification(icon, tickerText, when);
//     //定义下拉通知栏时要展现的内容信息
//     Context context = getApplicationContext();
//     CharSequence contentTitle = "我的通知栏标展开";
//     CharSequence contentText = "我的通知栏展开详细内容";
//     Intent notificationIntent = new Intent(AddressService.this,BootStartDemo.class);
//     PendingIntent contentIntent = PendingIntent.getActivity(AddressService.this, 0,notificationIntent, 0);
//     notification.setLatestEventInfo(context, contentTitle, contentText,contentIntent);
//     //用mNotificationManager的notify方法通知用户生成栏消息通知
//     mNotificationManager.notify(1, notification);
//   }
  }
  
  @Override
  public void onDestroy() {
    super.onDestroy();
    manager.listen(listener, PhoneStateListener.LISTEN_NONE);
    listener = null;
  }
  private class MyPhoneListener extends PhoneStateListener{
    
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
      super.onCallStateChanged(state, incomingNumber);
      switch (state) {
      case TelephonyManager.CALL_STATE_IDLE:
        if(null != view){
          wManager.removeView(view);
          view = null;
        }
        endTime = System.currentTimeMillis();
        break;
      case TelephonyManager.CALL_STATE_RINGING: // 零响状态
        //判断number是否在黑名单中
        if(dao.find(incomingNumber)){
          //挂断电话
          endCall();
          //删除呼叫记录
//         deleteCallLog(incomingNumber);
          ContentResolver resolver = getContentResolver();
          resolver.registerContentObserver(CallLog.Calls.CONTENT_URI, true, new MyObserver(new Handler(),incomingNumber));
        }
        Log.i(TAG,"来电号码为"+ incomingNumber);
        String address = NumberAddressService.getAddress(incomingNumber);
        Log.i(TAG,"归属地为"+ address);
        showLocation(address);
        //获取当前系统的时间
        startTime = System.currentTimeMillis();
        break;
      case TelephonyManager.CALL_STATE_OFFHOOK: //接通电话状态
        break;
      }
    }
  }
  
  public void showLocation(String address) {
    WindowManager.LayoutParams params = new WindowManager.LayoutParams();
    params.height = WindowManager.LayoutParams.WRAP_CONTENT;
    params.width = WindowManager.LayoutParams.WRAP_CONTENT;
    params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
        | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
        | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
    params.format = PixelFormat.TRANSLUCENT;
    params.type = WindowManager.LayoutParams.TYPE_TOAST;
    params.setTitle("Toast");
    params.gravity = Gravity.LEFT | Gravity.TOP;
    int x = sp.getInt("lastx", 0);
    int y = sp.getInt("lasty", 0);
    params.x = x;
    params.y = y;
    view = View.inflate(getApplicationContext(), R.layout.show_location, null);
    LinearLayout ll_location = (LinearLayout) view.findViewById(R.id.ll_location);
    TextView tv_location = (TextView) view.findViewById(R.id.tv_location);
    int background = sp.getInt("background", 0);
    if(0 == background){
      ll_location.setBackgroundResource(R.drawable.call_locate_gray);
    }else if(1 == background){
      ll_location.setBackgroundResource(R.drawable.call_locate_orange);
    }else {
      ll_location.setBackgroundResource(R.drawable.call_locate_green);
    }
    tv_location.setText(address);
    tv_location.setTextSize(24);
    wManager.addView(view, params);
  }
  
  public void deleteCallLog(String incomingNumber) {
    ContentResolver resolver = getContentResolver();
    Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI, null,"number=?", new String[]{incomingNumber}, null);
    if(cursor.moveToNext()){
      String id = cursor.getString(cursor.getColumnIndex("_id"));
      resolver.delete(CallLog.Calls.CONTENT_URI, "_id=?", new String[]{id});
    }
  }
  
  public void endCall() {
    try {
      Method method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
      IBinder binder = (IBinder) method.invoke(null, new Object[]{TELEPHONY_SERVICE});
      ITelephony telephony = ITelephony.Stub.asInterface(binder);
      telephony.endCall();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  private class MyObserver extends ContentObserver{
    private String incomingNumber;
    public MyObserver(Handler handler,String incomingNumber) {
      super(handler);
      this.incomingNumber = incomingNumber;
    }
    @Override
    public void onChange(boolean selfChange) {
      super.onChange(selfChange);
      deleteCallLog(incomingNumber);
      getContentResolver().unregisterContentObserver(this);
    }
  }
}

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android通信方式总结》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》

希望本文所述对大家Android程序设计有所帮助。

您可能感兴趣的文章:Android基于AudioManager、PhoneStateListener实现设置黑名单功能Android项目实现黑名单拦截效果


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯