简体   繁体   English

android检测来电和去电不适用于红米和OPPO手机

[英]android detecting incoming and outgoing calls is not working with redmi and oppo mobiles

//here is my code //这是我的代码

public class CallReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
            showToast(context,"Call started...");
        }
        else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE)){
            showToast(context,"Call ended...");
        }
        else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)){
            showToast(context,"Incoming call...");
        }
    }

    void showToast(Context context,String message){
        Toast toast=Toast.makeText(context,message,Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER,0,0);
        toast.show();
    }
}

//here i have registered my receiver in manifest //这里我已经在清单中注册了我的接收器

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

I am detecting incoming and outgoing calls in my android application this code is not working with the device redmi and oppo i want the code to be working for all the devices.I dont know what is the issue please help me out to fix this issue我在我的 android 应用程序中检测来电和去电 此代码不适用于设备 redmi 和 oppo 我希望代码适用于所有设备。我不知道是什么问题请帮我解决这个问题

As part of the Android 8.0 (API level 26) Background Execution Limits, apps that target the API level 26 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest.作为 Android 8.0(API 级别 26)后台执行限制的一部分,针对 API 级别 26 或更高级别的应用程序无法再在其清单中注册广播接收器以进行隐式广播。 you can register your receiver dynamically in Activity by calling registerReceiver(new CallReceiver(), new IntentFilter().addAction("android.intent.action.PHONE_STATE"));您可以通过调用registerReceiver(new CallReceiver(), new IntentFilter().addAction("android.intent.action.PHONE_STATE")); also, a bad thing about this approach is your receiver will only work when your app is in the foreground.此外,这种方法的一个坏处是您的接收器仅在您的应用程序处于前台时才能工作。 If you close your app then you don't trigger any broadcasts in background.如果您关闭您的应用程序,那么您不会在后台触发任何广播。 also, refer this if you are looking for call log data.另外,如果您正在查找通话记录数据,请参阅内容。

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

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