开发者

How do I get state of a outgoing call in android phone?

开发者 https://www.devze.com 2022-12-31 22:27 出处:网络
I noticed in the class TelephonyManager there are CALL_STATE_IDLE, CALL_STATE_OFFHOOK and CALL_STATE_RINGING. T开发者_开发知识库hey seem to be used for incoming calls.

I noticed in the class TelephonyManager there are CALL_STATE_IDLE, CALL_STATE_OFFHOOK and CALL_STATE_RINGING. T开发者_开发知识库hey seem to be used for incoming calls.

What I actually want to do is to be notified when an outgoing call is made, is received, or timed out. How to do that?


I don't know if you can detect a timed call, but differentiate when the call started is possible.

You can do it like this, in the 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)));
}

if duration is > 0 then then it call was answered.

Obviously there are other flags that you should use to determine that CALL_STATE_IDLE is called after a call was made.

Hope that helps and put you in the corret way for what you are trying to do.


From what I understand, you can detect that an outgoing call has been initiated because the phone state changes from idle to offhook. However, from there, knowing the state of that call- ie knowing if the call you are placing is ringing, being transferred to voice mail, actually picked up or just timed out appears to be something that we cannot detect.

Now I'm not sure if it is just undetectable in the SDK, but is communicated over the network and possibly detectable from the radio receiver itself, or if that information just plain isn't being transmitted.


The minimum that is need to do is:

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

精彩评论

暂无评论...
验证码 换一张
取 消