zoukankan      html  css  js  c++  java
  • Android监听电话

    public class PhoneBroadcast extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.e("onReceive", "onReceive    action" + intent.getAction());
    
            if (intent.getAction().equals("test")) {
                Log.e("test", "test");
                TelephonyManager tm = (TelephonyManager) context
                        .getSystemService(Context.TELEPHONY_SERVICE);
    
                PhoneStateListener listener = new PhoneStateListener() {
                    @Override
                    public void onCallStateChanged(int state, String incomingNumber) {
                        if (state != TelephonyManager.CALL_STATE_IDLE) {
                            Log.e("pause", "pause");
                        } else {
                            Log.e("start", "start");
                        }
    
                        super.onCallStateChanged(state, incomingNumber);
                    }
                };
                // 监听电话的状态
                tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
    
            }
        }
    }

    MainActivity

    public class MainActivity extends Activity {
    private
     PhoneBroadcast phone = new PhoneBroadcast();
    @Override protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
    }

    @Override
    public void onResume(){
       super.onResume();

       IntentFilter filter = new IntentFilter();
        filter.addAction("test");
        registerReceiver(phone, filter);
       Intent intent1 = new Intent("test");
       sendBroadcast(intent1);
    }
    @Override
    public void onPause(){
      super.onPause();
    unregisterReceiver(phone);
    }
    
    }

    AndroidMenifest.xml

     <receiver android:name="com.example.broadcasttest.PhoneBroadcast" >
            </receiver>

    权限

    <uses-permission android:name="android.permission.READ_PHONE_STATE" >
    </uses-permission>
    <uses-permission android:name="android.permission.CALL_PHONE" >
    </uses-permission>

  • 相关阅读:
    tile38 复制配置
    The Guardian’s Migration from MongoDB to PostgreSQL on Amazon RDS
    tile38 一款开源的geo 数据库
    sqler sql 转rest api 的docker 镜像构建(续)使用源码编译
    sqler sql 转rest api javascript 试用
    sqler sql 转rest api redis 接口使用
    sqler sql 转rest api 的docker image
    sqler sql 转rest api 的工具试用
    apache geode 试用
    benthos v1 的一些新功能
  • 原文地址:https://www.cnblogs.com/cc-Cheng/p/3146241.html
Copyright © 2011-2022 走看看