Android - 首次通话后启动意图

时间:2013-06-19 16:08:45

标签: java android android-intent telephonymanager

我正在开发一个应用程序,需要在第一次电话后启动意图 - 但不是在每次通话后。

以下来源应该在通话后启动一个意图 - 但如何修改它只进行一次(在第一次通话后)而不是每次通话后?

来源:

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

您可以简单地在持久存储中存储布尔标志(例如,SharedPreferences,文件或数据库),以指示是否已经检测到呼叫。将标志初始化为false并将代码包装在if语句中以检查标志是否为false。然后,在if语句中,将标记设置为true,您的代码将来不会输入if语句。