我在班上TelephonyManager注意到有CALL_STATE_IDLE,CALL_STATE_OFFHOOK和CALL_STATE_RINGING。他們似乎被用於來電。如何在android手機中獲得撥出電話的狀態?
我真正想要做的是,當傳出呼叫時被通知,被接收,或超時。怎麼做?
我在班上TelephonyManager注意到有CALL_STATE_IDLE,CALL_STATE_OFFHOOK和CALL_STATE_RINGING。他們似乎被用於來電。如何在android手機中獲得撥出電話的狀態?
我真正想要做的是,當傳出呼叫時被通知,被接收,或超時。怎麼做?
據我瞭解,你可以檢測呼出呼叫已啓動,因爲從空閒手機狀態變爲摘機。然而,從那裏,知道該呼叫的狀態 - 即知道您所撥打的電話是否正在響鈴,正在轉移到語音郵件,實際上已經啓動還是隻是超時,似乎是我們無法檢測到的。
現在,我不知道這是否是在SDK只是檢測不到,但傳遞通過網絡與可能從無線電接收器本身,或者,如果這些信息只是普通的沒有被傳輸檢測。
最小的是需要做的是:
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_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叫其他標誌。
希望有所幫助,並把你的corret方式爲你正在嘗試做的。
不要用這個解決方案浪費你的時間。持續時間永遠不會爲零。它將包括等待人員拿起電話的等待時間。最簡單的方法來驗證,就是撥打一個號碼,不要讓這個人接電話,然後讓手機超時或掛斷電話。您會注意到通話記錄中的持續時間不會爲零。 – AndroidDev 2013-03-05 14:08:28
@AndroidDev那麼我想那就取決於手機的型號,因爲我在某些手機上進行了測試,而一個未答覆的呼叫給出了一個持續時間爲0的日誌。 – htafoya 2013-03-05 16:12:20
代碼如何工作....? – 2011-09-19 05:01:23
這將永遠不會工作。當電話撥打外撥號碼時,立即呼叫CALL_STATE_OFFHOOK。 – AndroidDev 2013-03-05 14:12:36