如何在接听拨出电话时收到通知?

时间:2018-06-19 10:20:56

标签: android android-phone-call outgoing-call

背景

我需要在接听和挂断电话时收到通知。

对于来电,我使用TelephonyManager.ACTION_PHONE_STATE_CHANGED意图与其附带的TelephonyManager.EXTRA_STATE

问题

对于拨出电话,您在开始拨号后立即收到TelephonyManager.ACTION_PHONE_STATE_CHANGED的回叫,但在接听电话时却没有。

我通过互联网搜索过,并且都声称无法做到这一点。

但是,我注意到有些通话录音应用(例如" Boldbeast recorder")设法克服了这个问题,并以某种方式确定了通话应答的时刻。

enter image description here

我尝试了什么

我尝试注册ContentObserver来调用日志,但这没有帮助,因为它只在调用结束时才会收到回调。

要查看问题,请参阅我使用的一些代码:

MainActivity.kt

class MainActivity : AppCompatActivity() {
    companion object {
        private val REQUEST_CODE = 0
        @JvmStatic
        fun getAppDeclaredPermissions(context: Context): Array<out String>? {
            val pm = context.packageManager
            try {
                val packageInfo = pm.getPackageInfo(context.packageName, PackageManager.GET_PERMISSIONS)
                return packageInfo.requestedPermissions ?: return null
            } catch (ignored: PackageManager.NameNotFoundException) {
            }
            throw RuntimeException("cannot find current app?!")
        }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val permissionsToRequest = getAppDeclaredPermissions(this)
            requestPermissions(permissionsToRequest, 0)
        }
    }
}

PhoneBroadcastReceiver.kt

class PhoneBroadcastReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        val action = intent.action
        val state = intent.getStringExtra(TelephonyManager.EXTRA_STATE)
        val incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER)
        Log.d("AppLog", "PhoneBroadcastReceiver $action $state $incomingNumber")
    }
}

清单

<manifest package="com.example.user.myapplication" xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools">

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

    <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"
        tools:ignore="GoogleAppIndexingWarning">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>


        <receiver android:name=".PhoneBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>

问题

这些应用程序如何运作?如何在接听拨出电话时收到通知?

0 个答案:

没有答案