Android检测来电和去电在红米和Oppo手机上不起作用。

huangapple 未分类评论44阅读模式
英文:

Android detecting incoming and outgoing calls is not working with Redmi and Oppo mobiles

问题

以下是您的代码的翻译部分:

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,"通话已开始...");
        }
        else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE)){
            showToast(context,"通话已结束...");
        }
        else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)){
            showToast(context,"有来电...");
        }
    }

    void showToast(Context context, String message){
        Toast toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }
}
<!-- 在清单文件中注册接收器 -->
<receiver android:name=".CallReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.PHONE_STATE" />
    </intent-filter>
</receiver>

您在 Android 应用程序中检测来电和去电。此代码在 Redmi 和 Oppo 设备上不起作用,您希望代码在所有设备上都能正常工作。如何确定问题所在?

英文:

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,&quot;Call started...&quot;);
        }
        else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE)){
            showToast(context,&quot;Call ended...&quot;);
        }
        else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)){
            showToast(context,&quot;Incoming call...&quot;);
        }
    }

    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

  &lt;receiver android:name=&quot;.CallReceiver&quot;
            android:enabled=&quot;true&quot;
            android:exported=&quot;true&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.PHONE_STATE&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/receiver&gt;

I am detecting incoming and outgoing calls in my Android application. This code is not working with the device Redmi and Oppo, and I want the code to be working for all the devices. How can I determine what the issue is?

答案1

得分: 0

作为 Android 8.0(API 级别 26)后台执行限制的一部分,目标 API 级别在 26 或更高级别的应用程序不再可以在其清单中注册用于隐式广播的广播接收器。您可以通过在 Activity 中动态调用 registerReceiver(new CallReceiver(), new IntentFilter().addAction("android.intent.action.PHONE_STATE")); 来动态注册接收器。

另外,这种方法的一个不足之处是,接收器仅在应用程序处于前台时才起作用。如果关闭了应用程序,则不会在后台触发任何广播。如果您正在寻找通话日志数据,也可以参考此链接

英文:

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. you can register your receiver dynamically in Activity by calling registerReceiver(new CallReceiver(), new IntentFilter().addAction(&quot;android.intent.action.PHONE_STATE&quot;));
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.

huangapple
  • 本文由 发表于 2020年7月24日 12:39:30
  • 转载请务必保留本文链接:https://java.coder-hub.com/63066931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定