如何从PhoneStateListener的onCallStateChanged方法中获取TelephonyManager.CALL_STATE_OFFHOOK的实时状态。

问题描述 投票:1回答:1

我正在做一个拨打电话的代码。一切都很成功,除了我无法获得系统时间,当 TelephonyManager.CALL_STATE_OFFHOOK 国从 onCallStateChanged(int state, String incomingNumber).

同样地,我想得到系统时间,当 TelephonyManager.CALL_STATE_IDLE 状态已经到达。一旦我在Mainactivity中得到这两个值,我就可以得到差值,并根据它的值来启动其他活动。

例如,如果时间差在 TelephonyManager.CALL_STATE_IDLE 国家和 TelephonyManager.CALL_STATE_IDLE 状态小于30秒,那么我就可以认为这个电话没有被接听,我就可以开始另一个电话(或者打给另一个号码)。我可以在 onCallStateChanged 方法 PhoneStateListener 类,但无法通过变量或方法等方式将这些值传递给Mainactivity。但无法通过变量或方法等将这些值传递给Mainactivity。这是调用的代码,工作正常。

Intent myIntentCall = new Intent(Intent.ACTION_CALL, Uri.parse("tel:0123456789"));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)
                    != PackageManager.PERMISSION_GRANTED) {
    requestPermissions();
}
else
{
    startActivity(myIntentCall);            

}

private void requestPermissions () {
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 1);
}

从下面的代码中,我希望更新后的值是 _seconds 通过访问 PhoneReceiver._seconds 声明。但它始终保持 0. 但我得到的正确值是 _secondsPhoneReceiver 类。代码如下

public class PhoneReceiver extends PhoneStateListener {
    Context context;
    static boolean _callStarted = false;
    long _callStartTime;
    long _callEndTime;
    long _callDuration;
    long _minutes;
    static long _seconds;

    public PhoneReceiver(Context context) {
        this.context = context;
    }

   @Override
   public void onCallStateChanged(int state,  String incomingNumber) {
       if ((state == TelephonyManager.CALL_STATE_OFFHOOK) && !_callStarted) {
           _callStarted = !_callStarted;
           _callStartTime = new Date().getTime();
           Toast.makeText(context, "Stage 1: " + "Off Hook -> Boolean: "+_callStarted, Toast.LENGTH_LONG).show();
       }
       if ((state == TelephonyManager.CALL_STATE_IDLE) && _callStarted)
       {
            _callEndTime = new Date().getTime();
            _callDuration = _callEndTime - _callStartTime;
            _minutes = (_callDuration / 1000) / 60;
            _seconds = (_callDuration / 1000) % 60;
            Toast.makeText(context, "Stage 2: " + "IDLE State->Boolean: "+_callStarted, Toast.LENGTH_LONG).show();
       }
   }

}

Android Manifest.xml给定如下

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.callerApppackage.callerapp">

  <uses-permission android:name="android.permission.CALL_PHONE" />
  <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />

        </intent-filter>

    </activity>

    <!-- put this here so that even if the app is not running,
    your app can be woken up if there is a change in phone
    state -->
    <receiver android:name=".PhoneStateReceiver">
        <intent-filter>
            <action
                android:name=
                    "android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>


</application>

以下是PhonestateReciver类。

public class PhoneStateReceiver extends BroadcastReceiver {
    TelephonyManager manager;
    PhoneReceiver myPhoneStateListener;
    static boolean alreadyListening = false;

    @Override
    public void onReceive(Context context, Intent intent) {
        myPhoneStateListener = new PhoneReceiver(context);
        manager = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));

        //---do not add the listener more than once---
        if (!alreadyListening) {
            manager.listen(myPhoneStateListener,
                PhoneStateListener.LISTEN_CALL_STATE);
            alreadyListening = true;
        }
    }
}

希望我说清楚了 提前感谢大家的回复。

java android telephonymanager
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.