如何在Android手机中获得拨出电话的状态?

时间:2010-05-20 08:51:30

标签: android telephony phone-call

我注意到在类TelephonyManager中有CALL_STATE_IDLE,CALL_STATE_OFFHOOK和CALL_STATE_RINGING。它们似乎用于来电。

我真正想做的是在拨打电话,接听电话或超时时收到通知。怎么做?

3 个答案:

答案 0 :(得分:1)

根据我的理解,您可以检测到已启动拨出电话,因为电话状态从空闲状态变为摘机状态。但是,从那里,知道该呼叫的状态 - 即知道您正在拨打的电话是否正在振铃,转移到语音邮件,实际上是刚刚接听还是刚刚超时似乎是我们无法检测到的。

现在我不确定它是否只是在SDK中检测不到,而是通过网络进行通信,并且可能从无线电接收器本身检测到,或者如果没有传输该信息。

答案 1 :(得分:1)

需要做的最低限度是:

public class CallCounter extends PhoneStateListener {

    public void onCallStateChanged(int state, String incomingNumber) {
        switch(state) {
            case TelephonyManager.CALL_STATE_IDLE:
                    Log.d("Tony","Outgoing Call finished");
                    // Call Finished -> stop counter and store it.
                    callStop=new Date().getTime();
                    context.stopService(new Intent(context,ListenerContainer.class));

                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                    Log.d("Tony","Outgoing Call Starting");
                    // Call Started -> start counter.
                    // This is not precise, because it starts when calling,
                    // we can correct it later reading from call log
                    callStart=new Date().getTime();
                break;
        }
    }


public class ListenerContainer extends Service {
    public class LocalBinder extends Binder {
        ListenerContainer getService() {
            return ListenerContainer.this;
        }
    }
    @Override
    public void onStart(Intent intent, int startId) {
        TelephonyManager tManager =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        CallCounter callCounter=new CallCounter(this);
        tManager.listen(callCounter,PhoneStateListener.LISTEN_CALL_STATE);
        Log.d("Tony","Call COUNTER Registered");
    }
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
    // This is the object that receives interactions from clients.  See
    // RemoteService for a more complete example.
    private final IBinder mBinder = new LocalBinder();

}

public class myReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
            context.startService(new Intent(context,ListenerContainer.class));
        }
        }
}

答案 2 :(得分:1)

我不知道您是否可以检测到定时通话,但可以区分启动通话的时间。

您可以在CALL_STATE_IDLE:

中执行此操作
Uri allCalls = Uri.parse("content://call_log/calls");
String lastMinute = String.valueOf(new Date().getTime() - DAY_IN_MILISECONDS); 
//before the call started
Cursor c = app.getContentResolver().query(allCalls, null, Calls.DATE + " > " 
           + lastMinute, null, Calls.DATE + " desc");
c.moveToFirst();

if (c.getCount() > 0) {
    int duration = Integer.parseInt(c.getString(c.getColumnIndex(Calls.DURATION)));
}

如果持续时间> 0然后它的电话被接听了。

显然,还有其他标志可用于确定在调用后调用CALL_STATE_IDLE。

希望能帮助你并使你正确地处理你想要做的事情。