简体   繁体   English

即使应用程序终止,如何在 Android 中检测来电

[英]How to detect incoming calls in Android even when the app is terminated

I have a broadcast receiver which detects incoming calls and starts an activity.我有一个广播接收器,它可以检测来电并启动一项活动。 But it doesnt work sometimes in real devices.但它有时在真实设备中不起作用。 How can I make it work all the time?我怎样才能让它一直工作?

My receiver:我的接收器:

        <receiver android:name="InterceptCall"
            android:exported="true"
            android:enabled="true"
            >
            <intent-filter android:priority="999">
                <action android:name="android.intent.action.PHONE_STATE"/>
            </intent-filter>
        </receiver>

onReceive method onReceive 方法

    @Override
    override fun onReceive(context: Context?, intent: Intent) {
        try {
            val state: String? = intent.getStringExtra(TelephonyManager.EXTRA_STATE)
            val incomingNumber: String? =
                intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER)
            if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                Log.d("INFO", incomingNumber.toString())
                Toast.makeText(context, "Gelen Arama $incomingNumber", Toast.LENGTH_SHORT).show()
                if(incomingNumber != null){
                    simpleFloatingWindow = SimpleFloatingWindow(context!!)
                    simpleFloatingWindow.show()
                }
            }
            if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
                //Toast.makeText(context,"Call Received State",Toast.LENGTH_SHORT).show();
            }
            if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
                //Toast.makeText(context,"Call Idle State",Toast.LENGTH_SHORT).show();
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

You can't start an activity from the background from Android 10+ devices.您无法从 Android 10+ 设备的后台启动活动。 Except for the cases:除了以下情况:

https://developer.android.com/guide/components/activities/background-starts#exceptions https://developer.android.com/guide/components/activities/background-starts#exceptions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM