我想知道在android程序中是否接听了拨出电话。我只是想知道拨出的电话是否有答案。我是android的新手。请帮我解决这个问题。接听电话时间我用这个。
我想知道在android程序中是否接听了拨出电话。我只是想知道外出电话是否已经回答。我是android新手。请帮我解决这个问题。为了获得呼叫应答时间,我使用它。
BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
} else {
String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
int state = 0;
if (stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
state = TelephonyManager.CALL_STATE_IDLE;
} else if (stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
state = TelephonyManager.CALL_STATE_OFFHOOK;
} else if (stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
state = TelephonyManager.CALL_STATE_RINGING;
}
onCallStateChanged(context, state, number);
}
}
};
protected void onIncomingCallStarted(Context ctx, String number, Date start) {
}
protected void onOutgoingCallStarted(Context ctx, String number, Date start) {
}
protected void onIncomingCallEnded(Context ctx, String number, Date start, Date end) {
}
protected void onOutgoingCallEnded(Context ctx, String number, Date start, Date end) {
SimpleDateFormat localDateFormat = new SimpleDateFormat("HH:mm:ss");
startime = localDateFormat.format(start);
endtime = localDateFormat.format(end);
long aa = start.getTime();
long bb = end.getTime();
long cc = bb - aa;
long diffMinutes = cc / 1000 % 60;
duration = Long.toString(diffMinutes);
Bundle bundle = new Bundle();
bundle.putString("start", startime);
bundle.putString("end", endtime);
bundle.putString("duration", duration);
Call_End dFragment = new Call_End();
dFragment.setArguments(bundle);
dFragment.show(fragmentManager, "Dialog Fragment");
}
protected void onMissedCall(Context ctx, String number, Date start) {
}
public void onCallStateChanged(Context context, int state, String number) {
if (lastState == state) {
//No change, debounce extras
return;
}
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
isIncoming = true;
callStartTime = new Date();
savedNumber = number;
onIncomingCallStarted(context, number, callStartTime);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
callStartTime = new Date();
onOutgoingCallStarted(context, savedNumber, callStartTime);
//Transition of ringing->offhook are pickups of incoming calls. Nothing done on them
if (lastState != TelephonyManager.CALL_STATE_RINGING) {
isIncoming = false;
callStartTime = new Date();
onOutgoingCallStarted(context, savedNumber, callStartTime);
}
break;
case TelephonyManager.CALL_STATE_IDLE:
//Went to idle- this is the end of a call. What type depends on previous state(s)
if (lastState == TelephonyManager.CALL_STATE_RINGING) {
//Ring but no pickup- a miss
onMissedCall(context, savedNumber, callStartTime);
} else if (isIncoming) {
onIncomingCallEnded(context, savedNumber, callStartTime, new Date());
} else {
onOutgoingCallEnded(context, savedNumber, callStartTime, new Date());
}
break;
}
lastState = state;
}
答案 0 :(得分:0)
@Override
public void onReceive(Context context, Intent intent) {
Log.d("Intent", intent.toString());
if(intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL"))
{
Log.e("Call_state","outgoing call detected");
}
// Checking for the call status
try {
// TELEPHONY MANAGER class object to register one listner
TelephonyManager tmgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
//Create Listner
MyPhoneStateListener PhoneListener = new MyPhoneStateListener();
// Register listener for LISTEN_CALL_STATE
tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
} catch (Exception e) {
e.printStackTrace();
}
}
private class MyPhoneStateListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
Log.e("Call_state","call ringing");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.e("Call_state","call offhook");
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.e("Call_state","call idle");
break;
}
}
}
答案 1 :(得分:0)
你可以这样做,但这不适用于所有手机。
创建通知侦听器服务,告知其他人接听电话。public class CallNotification extends NotificationListenerService {
public void onNotificationPosted(StatusBarNotification sbn)
{
//your code here after you getting notification another person has pick up the call
}
}
要求系统发出允许通知。
Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
startActivity(intent);