在Android

时间:2018-07-17 01:04:52

标签: android android-studio

我制作了这个Android应用程序,该应用程序允许用户在每个来电结束时保存有关来电者的一​​些数据,因此当来电结束时,会出现一个弹出窗口,询问用户是否要保存一些信息关于这个数字。 所以我有这堂课

public abstract class PhonecallReceiver extends BroadcastReceiver {



    private static int lastState = TelephonyManager.CALL_STATE_IDLE;
    private static Date callStartTime;
    private static boolean isIncoming;
    private static String savedNumber;  

    @Override
    public void onReceive(Context context, Intent intent) {

        //We listen to two intents.  The new outgoing call only tells us of an outgoing call.  We use it to get the number.
        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);
        }
    }

    //Derived classes should override these to respond to specific events of interest
    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){}
    protected void onMissedCall(Context ctx, String number, Date start){}

    //Deals with actual events

    //Incoming call-  goes from IDLE to RINGING when it rings, to OFFHOOK when it's answered, to IDLE when its hung up
    //Outgoing call-  goes from IDLE to OFFHOOK when it dials out, to IDLE when hung up
    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:
                //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());  
 Intent trIntent = new Intent("android.intent.action.MAIN");
                                trIntent.setClass(context, YourDialog.class);
                                trIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                context.startActivity(trIntent);                     
                }
                else{
                    onOutgoingCallEnded(context, savedNumber, callStartTime, new Date());                                               
                }
                break;
        }
        lastState = state;
    }
}

您的对话框类是弹出的界面,并以EndNumber作为数字来保存有关 现在,这在单个呼叫中就可以完美运行,这意味着用户接听电话并讲话,然后挂断弹出的提示音,一切正常,但是当用户处于呼叫中间并且有人给他打电话时,如果用户挂断第二个呼叫并在挂断电话时完成第一个呼叫,而这是第二个呼叫者的电话,则该问题出在第二个呼叫者的电话上问题是,当用户处于打入电话的中间位置,而其他人接听他的电话时,第二个呼叫者会自动挂断第一个呼叫者,而当第二个呼叫结束时,弹出窗口仅显示第二个电话的号码,应该在第一个电话显示便便,而另一个在第二个电话显示便便。 我希望我能适当地解释一下它的混乱之处,但是如果有人能够帮助我弄清楚,那就太好了

0 个答案:

没有答案