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

    android的电话监听

    新建一个项目,结构图如下:

    PhoneService:
    package com.demo.tingdianhua;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    import android.telecom.TelecomManager;
    import android.telephony.PhoneStateListener;
    import android.telephony.TelephonyManager;
    
    public class PhoneService extends Service {
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO: Return the communication channel to the service.
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
    
        @Override
        public void onCreate() {
    
            System.out.println("onCreate");
            //1.获取telephone的实例
            TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    
            //2.注册电话的监听
            tm.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);
    
    
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
        }
    
    
        public class MyPhoneStateListener extends PhoneStateListener {
    
            //当电话设置状态发生改变的时候调用
            public void onCallStateChanged(int state, String phoneNumber) {
                System.out.println("state==>"+state);
                System.out.println("phoneNumber==>"+phoneNumber);
    
    
                //具体判断下电话得状态
                switch (state) {
                    case TelephonyManager.CALL_STATE_IDLE:
    
                        System.out.println("停止");
                        break;
    
                    case TelephonyManager.CALL_STATE_OFFHOOK://接听状态
                        System.out.println("开始录");
    
                        break;
    
    
                    case TelephonyManager.CALL_STATE_RINGING://电话响铃状态
    
    
                        System.out.println("准备一个录音机");
    
    
                        break;
    
                    default:
    
    
                        break;
    
    
                }
    
    
                super.onCallStateChanged(state, phoneNumber);
            }
        }
    
    
    }
    
    
    MainActivity:
    package com.demo.tingdianhua;
    
    import android.content.Intent;
    import android.support.v4.app.ActivityCompat;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    
    public class MainActivity extends AppCompatActivity {
    
        private String [] phone={
                "android.permission.READ_PHONE_STATE"
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            ActivityCompat.requestPermissions(this,phone,1);
        }
    
        public void clickphone(View view) {
            Intent intent = new Intent(MainActivity.this,PhoneService.class);
            startService(intent);
        }
    }
    
    
    activity_main.xml:
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <Button
            android:onClick="clickphone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="开启服务"
            />
    
    </android.support.constraint.ConstraintLayout>
    
    
    AndroidManifest.xml:
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.demo.tingdianhua">
    
        <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            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>
    
            <service
                android:name=".PhoneService"
                android:enabled="true"
                android:exported="true"></service>
        </application>
    
    </manifest>
    
  • 相关阅读:
    常用配色方案
    js一个典型的对象写法,推荐使用这种格式,用于处理图像的基本方法、
    jquery 插件开发分析
    理解js 的作用域链 原型链 闭包 词法分析
    根据入栈判断出栈是否合法
    动态规划之数字三角形(三种解法:递归,递推,记忆化搜索)
    memset用法详解
    刘汝佳算法竞赛入门经典中的运算符>?问题
    算法之递推及其应用(递推关系的建立及在信息学竞赛中的应用 安徽 高寒蕊)
    自适应流体布局
  • 原文地址:https://www.cnblogs.com/charlypage/p/10316719.html
Copyright © 2011-2022 走看看