我正在开发一个应用程序,该应用程序需要在第一个电话之后启动意图,但不必在每次电话后都启动意图。

以下来源应在通话后启动意图-但是如何将其修改为仅在一次通话之后(而不是在每次通话后)执行一次?

资源:

case TelephonyManager.CALL_STATE_RINGING:
            Toast.makeText( context, "incoming call", Toast.LENGTH_LONG).show();
            IntentService = new Intent(context, PlayService.class).setAction("incoming_call");
            IntentService.putExtra("phone_number",intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER) );
            if (SmsReceiver.bool)
            context.startService(IntentService);
            break;


或者也许我可以使用:

EndCallListener callListener = new EndCallListener();
TelephonyManager mTM = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);

private class EndCallListener extends PhoneStateListener {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        if(TelephonyManager.CALL_STATE_RINGING == state) {
            Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
        }
        if(TelephonyManager.CALL_STATE_OFFHOOK == state) {
            //wait for phone to go offhook (probably set a boolean flag) so you know your app initiated the call.
            Log.i(LOG_TAG, "OFFHOOK");
        }
        if(TelephonyManager.CALL_STATE_IDLE == state) {
            //when this state occurs, and your flag is set, restart your app
            Log.i(LOG_TAG, "IDLE");
        }
    }
}

最佳答案

您可以使用Shared Preferences跟踪已拨打的电话数。

09-12 09:17