zoukankan      html  css  js  c++  java
  • 监听TelephonyManager的通话状态来监听手机的所有的来电

    import java.io.FileNotFoundException;
    import java.io.OutputStream;
    import java.io.PrintStream;
    import java.util.Date;

    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Context;
    import android.telephony.PhoneStateListener;
    import android.telephony.TelephonyManager;
    import android.view.Menu;

    public class MonitorPhone extends Activity {
      TelephonyManager tManager;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_monitor_phone);
        //获取TelephonyManager对象
        tManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        //创建一个通话状态监听器
        PhoneStateListener listener = new 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:
                OutputStream os = null;
                try {
                  os = openFileOutput("phoneList", MODE_APPEND);
                } catch (FileNotFoundException e) {
                  e.printStackTrace();
                }
                PrintStream ps = new PrintStream(os);
                //将来电号码记录到文件中
                ps.println(new Date() + "来电:"+incomingNumber);
                ps.close();
                break;
              default:
                break;
            }
            super.onCallStateChanged(state, incomingNumber);
          }
        };
        //监听电话通话状态的改变
        tManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
       }

    }

  • 相关阅读:
    根据 谷歌官网写一个 该浏览器上的扩展
    深度学习
    华为在线学习
    pandas 常规操作大全
    猎奇有些意思的
    LINUX 必知必会检测表--通读无关语言
    自动化部署三剑客 gitlab + ansible + jenkins
    python 框架
    delphi 多线程之System.TMonitor
    函数和对象 及 prototype和__proto__
  • 原文地址:https://www.cnblogs.com/jiww/p/5607212.html
Copyright © 2011-2022 走看看