Java android系统中的来电广播接收器';行不通

Java android系统中的来电广播接收器';行不通,java,android,broadcastreceiver,android-broadcastreceiver,incoming-call,Java,Android,Broadcastreceiver,Android Broadcastreceiver,Incoming Call,我正在尝试开发一个android应用程序,可以记录电话。所以,在最初的步骤中,我必须看看BroadcastReceiver是否被解雇。 我在AndroidManifest文件中添加了权限、接收者标记。我正在OnePlus X上测试。活动已开始,但当我接到呼叫时,BroadcastReceiver不会被触发。这里怎么了 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.an

我正在尝试开发一个android应用程序,可以记录电话。所以,在最初的步骤中,我必须看看BroadcastReceiver是否被解雇。 我在AndroidManifest文件中添加了权限、接收者标记。我正在OnePlus X上测试。活动已开始,但当我接到呼叫时,BroadcastReceiver不会被触发。这里怎么了

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity
        android:name="com.example.myapp.MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name=".PhoneStateReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>

</application>

</manifest>
该权限是一个危险的权限,如果您使用的是棉花糖设备,则必须请求运行时权限,否则您的广播接收器将无法工作,也不会抛出错误


这很可能是您的代码出现问题的原因,因为您已正确地在意向过滤器中注册,除了没有任何错误,因为它只是一个广播接收器,应该可以工作,即被Android系统调用。

检查此项权限在哪里?@EliasFazel SecondLine(从底部开始)。我现在将其移到顶部,因为android studio报告了该顺序错误。@Oussemaroua我已经尝试了该链接中的代码。仍然没有结果。可能是电话问题吗?我不这么认为,你添加了第二个权限了吗?
package com.example.myapp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class PhoneStateReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        try {
            System.out.println("Receiver start");
            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
            String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

            if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
                Toast.makeText(context,"Incoming Call State",Toast.LENGTH_SHORT).show();
                Toast.makeText(context,"Ringing State Number is -"+incomingNumber,Toast.LENGTH_SHORT).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 (Exception e){
            e.printStackTrace();
        }

    }
}