【问题标题】:How to start a timer when a call is get connected?通话接通时如何启动计时器?
【发布时间】:2015-11-11 04:43:09
【问题描述】:

我需要在通话接通时启动计时器,但在我的情况下,它是在通话响铃时启动的,我搜索了很多,但没有得到正确的解决方案。我再次怀着希望重复这一点。至少我需要一个建议来实现这一点。

我的代码:

public void onReceive(Context context, Intent intent) {

    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

    if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {    
        Log.d("Status", "Phone is Ringing");
    } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {    
        Log.d("Status", "Phone is on call");
    } else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
        // Call Dropped or rejected   

        System.exit(0);
        Log.d("Status", "Phone is dropped");
    }    
}

【问题讨论】:

    标签: java android call telephonymanager phone-state-listener


    【解决方案1】:

    你可以试试这个。 在您的广播接收器中

    static boolean flag = false;
    static long start_time, end_time;
    

    而onReceive函数会是这样的,

    public void onReceive(Context arg0, Intent intent) {
            String action = intent.getAction();
            if (action.equalsIgnoreCase("android.intent.action.PHONE_STATE")) {
                if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                TelephonyManager.EXTRA_STATE_RINGING)) {
                    start_time = System.currentTimeMillis();
                }
                if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                TelephonyManager.EXTRA_STATE_IDLE)) {
                    end_time = System.currentTimeMillis();
                    //Total time talked =
                    long total_time = end_time - start_time;
                    //Store total_time somewhere or pass it to an Activity using intent
                }
            }
    

    您还需要在清单中为此授予权限。

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    

    像这样在你的 Manifest 中注册一个接收者:

     <receiver android:name=".CallDurationReceiver">
           <intent-filter>
               <action android:name="android.intent.action.PHONE_STATE" />
           </intent-filter>
        </receiver>
    

    【讨论】:

    • 定时器意味着我需要开始一个新的活动,我正在开始定时器。
    • 你要开始什么活动?
    • 我的问题是我需要在用户收到 cal 时启动我的简单应用程序。我正在启动计时器。此处我的活动仅在传出呼叫响铃时开始
    • 所以当用户收到时我需要准确的状态
    猜你喜欢
    • 1970-01-01
    • 2014-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    相关资源
    最近更新 更多