如何在Android手机中获取呼出电话的状态?

6

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

实际上,我想要做的是在呼出电话、接听电话或超时时得到通知。该如何实现?

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在通话结束后被调用。

希望这能帮助你,并使你在尝试做某事时走上正确的道路。


不要浪费时间在这个解决方案上。持续时间永远不会为零。它将包括等待时间,直到有人接电话。验证的最简单方法是只拨一个号码,不让对方接听,然后让电话超时或自己挂断。您会注意到通话记录中的持续时间不为零。 - Johann
1
@AndroidDev 好吧,我猜这取决于手机的型号,因为我在一些手机上测试过,未接来电会显示0秒的通话记录。 - htafoya

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

这永远不会起作用。当手机拨打外拨号码时,CALL_STATE_OFFHOOK立即被调用。 - Johann

1
据我所知,您可以检测到呼出电话已启动,因为手机状态从空闲变为摘机。然而,从那里开始,了解该通话的状态-即知道您正在拨打的电话是响铃、被转移到语音邮件、实际接听还是超时等似乎是我们无法检测到的。现在我不确定它是否只是在SDK中无法检测到,但是通过网络传输并且可能可以从无线电接收器本身检测到,或者该信息根本没有被传输。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接