6

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

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

4

3 回答 3

2

我不知道您是否可以检测到定时呼叫,但请区分何时可以开始呼叫。

您可以在 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。

希望对您有所帮助,并为您尝试做的事情提供正确的方式。

于 2012-07-25T03:05:50.410 回答
1

据我了解,您可以检测到已拨出电话,因为电话状态从空闲变为摘机。然而,从那里,知道该呼叫的状态——即知道您所拨打的呼叫是否正在响铃、是否被转移到语音邮件、实际被接听或只是超时似乎是我们无法检测到的事情。

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

于 2010-07-13T15:48:35.190 回答
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));
        }
        }
}
于 2010-08-31T11:14:01.057 回答