[英]How to start a timer when a call is get connected?
我需要在通話接通時啟動一個計時器,但就我而言,它是在電話響鈴時啟動的,我進行了很多搜索,但沒有找到合適的解決方案。 再次懷着希望,我在重復這一點。 至少我需要一個建議來實現這一目標。
我的代碼:
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");
}
}
您可以嘗試一下。 在您的BroadCastReceiver中
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" />
像這樣在清單中注冊一個接收器:
<receiver android:name=".CallDurationReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.