android中的广播接收器不工作

时间:2017-04-28 05:12:24

标签: java android broadcastreceiver

我创建了一个使用BroadcastReceiver跟踪来电和去电的应用。我的问题是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。

2 个答案:

答案 0 :(得分:2)

在onCreate中注册Receiver并在Activity的onDestroy中注销它。但是你的要求是它应该工作,即使你的活动已经关闭,所以要提供服务然后去做。

有关其他详细信息,请参阅此代码

CallStateService

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;
    }
}
}

CallHepler

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);
}

}

Outgoing Reciever

/**
 * 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一次。然后一切正常。 事先给予权限。

答案 1 :(得分:1)

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