Java android中的广播接收器不工作

Java android中的广播接收器不工作,java,android,broadcastreceiver,Java,Android,Broadcastreceiver,我已经创建了一个应用程序,它使用广播接收器跟踪传入和传出的呼叫。我的问题是BrocastReceiver不工作,并且没有跟踪传入和传出的呼叫。即使我的应用程序关闭,也应该跟踪调用 //MainActivity.java public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.

我已经创建了一个应用程序,它使用
广播接收器
跟踪传入和传出的呼叫。我的问题是
BrocastReceiver
不工作,并且没有跟踪传入和传出的呼叫。即使我的应用程序关闭,也应该跟踪调用

//MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
//Calls.java

public abstract class Calls extends BroadcastReceiver {
    private static int lastState = TelephonyManager.CALL_STATE_IDLE;
    private static Date callStartTime;
    private static boolean isIncoming;
    private static String savedNumber;

    @Override
    public void onReceive(Context context, Intent intent) {            
    if(intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
        savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
    }
    else {
        String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
        String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
        int state = 0;
        if(stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
            state = TelephonyManager.CALL_STATE_IDLE;
        }
        else if(stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
            state = TelephonyManager.CALL_STATE_OFFHOOK;
        }
        else if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
            state = TelephonyManager.CALL_STATE_RINGING;
        }
        onCallStateChanged(context, state, number);
    }
}

protected abstract void onIncomingCallReceived(Context ctx, String number, Date start);
protected abstract void onIncomingCallAnswered(Context ctx, String number, Date start);
protected abstract void onIncomingCallEnded(Context ctx, String number, Date start, Date end);
protected abstract void onOutgoingCallStarted(Context ctx, String number, Date start);
protected abstract void onOutgoingCallEnded(Context ctx, String number, Date start, Date end);
protected abstract void onMissedCall(Context ctx, String number, Date start);

public void onCallStateChanged(Context context, int state, String number) {
    if(lastState == state) {
        //No change, debounce extras
        return;
    }
    switch (state) {
        case TelephonyManager.CALL_STATE_RINGING:
            isIncoming = true;
            callStartTime = new Date();
            savedNumber = number;
            onIncomingCallReceived(context, number, callStartTime);
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            //Transition of ringing->offhook are pickups of incoming calls.  Nothing done on them
            if(lastState != TelephonyManager.CALL_STATE_RINGING) {
                isIncoming = false;
                callStartTime = new Date();
                onOutgoingCallStarted(context, savedNumber, callStartTime);
            }
            else {
                isIncoming = true;
                callStartTime = new Date();
                onIncomingCallAnswered(context, savedNumber, callStartTime);
            }
            break;
        case TelephonyManager.CALL_STATE_IDLE:
            //Went to idle-  this is the end of a call.  What type depends on previous state(s)
            if(lastState == TelephonyManager.CALL_STATE_RINGING) {
                //Ring but no pickup-  a miss
                onMissedCall(context, savedNumber, callStartTime);
            }
            else if(isIncoming) {
                onIncomingCallEnded(context, savedNumber, callStartTime, new Date());
            }
            else {
                onOutgoingCallEnded(context, savedNumber, callStartTime, new Date());
            }
            break;
    }
    lastState = state;
}
}
//CallReceiver.java

public class CallReceiver extends Calls {
@Override
protected void onIncomingCallReceived(Context ctx, String number, Date start) {
    Toast.makeText(ctx, ""+number, Toast.LENGTH_LONG).show();
}

@Override
protected void onIncomingCallAnswered(Context ctx, String number, Date start) {
    Toast.makeText(ctx, ""+number, Toast.LENGTH_LONG).show();
}

@Override
protected void onIncomingCallEnded(Context ctx, String number, Date start, Date end) {
    Toast.makeText(ctx, ""+number, Toast.LENGTH_LONG).show();
}

@Override
protected void onOutgoingCallStarted(Context ctx, String number, Date start) {
    Toast.makeText(ctx, ""+number, Toast.LENGTH_LONG).show();
}

@Override
protected void onOutgoingCallEnded(Context ctx, String number, Date start, Date end) {
    Toast.makeText(ctx, ""+number, Toast.LENGTH_LONG).show();
}

@Override
protected void onMissedCall(Context ctx, String number, Date start) {
    Toast.makeText(ctx, ""+number, Toast.LENGTH_LONG).show();
}
}
//AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.example.hawkeye">

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<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"
    android:fullBackupContent="true">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

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

</manifest>


我哪里做错了。有什么解决办法吗。我不知道如何解决这个问题。我是android新手。Thanyou.

使用其他活动或服务某些系统接收器(如电池电量等)必须注册活动或服务

使用其他活动或服务某些系统接收器(如电池电量等)必须注册“活动”或“服务”

在“活动”的onCreate中注册接收者,并在onDestroy中注销。但您的要求是,即使您的“活动”已关闭,它也应工作,因此,请创建一个服务并执行它

有关更多详细信息,请参阅此代码

呼叫状态服务

public class CallStateService extends Service {
private CallHelper callHelper;

public CallStateService() {
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d("Ispy","Callservice Start");
    callHelper = new CallHelper(this);

    int res = super.onStartCommand(intent, flags, startId);
    callHelper.start();
    return res;
 }

@Override
public void onDestroy() {
    super.onDestroy();

    callHelper.stop();
}

@Override
public IBinder onBind(Intent intent) {
    // not supporting binding
    return null;
}
}

CallStateListener

/**
 * Listener to detect incoming calls.
 */
 class CallStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
    switch (state) {
        case TelephonyManager.CALL_STATE_RINGING:


            Log.d("Incoming number",incomingNumber);
            break;
    }
}
}
卡勒普勒

public class CallHelper {

private Context ctx;
private TelephonyManager tm;
private CallStateListener callStateListener;

private OutgoingReceiver outgoingReceiver;

public CallHelper(Context ctx) {
    this.ctx = ctx;

    callStateListener = new CallStateListener();
    outgoingReceiver = new OutgoingReceiver();
}

/**
 * Start calls detection.
 */
public void start() {
    tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
    tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);

    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);
    ctx.registerReceiver(outgoingReceiver, intentFilter);
}

/**
 * Stop calls detection.
 */
public void stop() {
    tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
    ctx.unregisterReceiver(outgoingReceiver);
}

}
输出接收器

/**
 * Broadcast receiver to detect the outgoing calls. Use yours Instead but make sure to register in callhelper
 */
public class OutgoingReceiver extends BroadcastReceiver {
public OutgoingReceiver() {
}

@Override
public void onReceive(Context context, Intent intent) {
    String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
    Log.d("Outgoing number",number);


}
}
并从活动中启动CallStateService一次,然后一切正常。
事先授予权限。

在onCreate中注册接收者,并在活动的onDestroy中注销它。但您的要求是,即使您的活动已关闭,它也应该工作,所以请创建一个服务并执行它

有关更多详细信息,请参阅此代码

呼叫状态服务

public class CallStateService extends Service {
private CallHelper callHelper;

public CallStateService() {
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d("Ispy","Callservice Start");
    callHelper = new CallHelper(this);

    int res = super.onStartCommand(intent, flags, startId);
    callHelper.start();
    return res;
 }

@Override
public void onDestroy() {
    super.onDestroy();

    callHelper.stop();
}

@Override
public IBinder onBind(Intent intent) {
    // not supporting binding
    return null;
}
}

CallStateListener

/**
 * Listener to detect incoming calls.
 */
 class CallStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
    switch (state) {
        case TelephonyManager.CALL_STATE_RINGING:


            Log.d("Incoming number",incomingNumber);
            break;
    }
}
}
卡勒普勒

public class CallHelper {

private Context ctx;
private TelephonyManager tm;
private CallStateListener callStateListener;

private OutgoingReceiver outgoingReceiver;

public CallHelper(Context ctx) {
    this.ctx = ctx;

    callStateListener = new CallStateListener();
    outgoingReceiver = new OutgoingReceiver();
}

/**
 * Start calls detection.
 */
public void start() {
    tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
    tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);

    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);
    ctx.registerReceiver(outgoingReceiver, intentFilter);
}

/**
 * Stop calls detection.
 */
public void stop() {
    tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
    ctx.unregisterReceiver(outgoingReceiver);
}

}
输出接收器

/**
 * Broadcast receiver to detect the outgoing calls. Use yours Instead but make sure to register in callhelper
 */
public class OutgoingReceiver extends BroadcastReceiver {
public OutgoingReceiver() {
}

@Override
public void onReceive(Context context, Intent intent) {
    String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
    Log.d("Outgoing number",number);


}
}
并从活动中启动CallStateService一次,然后一切正常。
事先给予许可。

你在使用哪台设备?@AbdulKawee:Google NexusI我在Google nexus 5中使用了这段代码。它的工作原理是什么“不工作”意思是?@代码学徒:没有收到任何呼入和呼出电话的吐司通知。您使用的是哪种设备?@AbdulKawee:Google NexusI我在Google nexus 5中使用此代码其工作精细“不工作”是什么意思?@代码学徒:没有收到呼入和呼出电话的吐司通知。