Amani Swann
Amani Swann

Reputation: 11

Android - Starting an Intent After the First PhoneCall

I'm developing an application which needs to start an intent after the first phone call - but not after every phone call.

The following source should start an intent after a call - but how can it be modified to only do it once (after the first call is made) instead of after every call?

SOURCE:

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;

or perhaps I could use:

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

Upvotes: 0

Views: 82

Answers (2)

HexAndBugs
HexAndBugs

Reputation: 5789

You can simply store a Boolean flag in persistent storage (e.g. SharedPreferences, file or database) to indicate if a call has been detected already. Initialise the flag to false and wrap your code in an if statement to check if the flag is false. Then, within the if statement, set the flag to true and your code won't enter the if statement in future.

Upvotes: 0

Leo Landau
Leo Landau

Reputation: 1795

You can use Shared Preferences to keep track of the number of calls made.

Upvotes: 1

Related Questions