How to finish activity in broadcastreceiver onCallEnded()

亡梦爱人 提交于 2019-12-11 19:13:29

问题


I have a question, how to finish activity in brodcastreceiver onCallEnded (SIP) . There is a brodcastreceiver like this:

public class IncomingCallReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
         SipAudioCall incomingCall = null;
        try {

            SipAudioCall.Listener listener = new SipAudioCall.Listener() {

                ...

                @Override
                public void onCallEnded(SipAudioCall call) {

                 // IncomingCallActivity.finish();

                }
            };

            Main mainActivity = (Main) context;
            incomingCall = mainActivity.manager.takeAudioCall(intent, listener);
            mainActivity.call = incomingCall;

            Intent i = new Intent(context, IncomingCallActivity.class);  
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
            context.startActivity(i);

Then start new activity to answer or reject call, but how to close it when call is finished? Cant use IncomingCallActivity.finish() in onCallEnded.


回答1:


First why can you add the listener in IncomingCallActivity itself. I dont think that will make any difference.

Second for your solution,

start your activity with a FLAG_ACTIVITY_SINGLE_TOP so that you receive the new Intent in onNewIntent(Intent intent) of the activity.

Then onCallEnded start the activity once again.

Intent i = new Intent(context, IncomingCallActivity.class);  
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);  
i.putExtra("Close", true); // an extra which says to finish the activity.
context.startActivity(i);

Now in onNewIntent(Intent intent)

if(intent.getBooleanExtra("Close", false))
  finish();



回答2:


You can do it like this:

    @Override
            public void onCallEnded(SipAudioCall call) {

             // IncomingCallActivity.finish();
             //Here set any value like an int
             intToClose = 1;

            }

then in your activity check for the intToClose, if its 1 then just call fnish();

UPDATE: This code is from my mind, so it can have some errors. Add this to your current activity, or something like this:

scheduler = Executors.newSingleThreadScheduledExecutor ( );
                    scheduler.scheduleAtFixedRate ( new Runnable ( ) {
                public void run ( )
                    {
                      runOnUiThread ( new Runnable ( ) {
                      public void run ( ) {
                      closeInt = IncomingCallReceiver.intToClose;
                      if ( closeInt == 1 ) {    
                          scheduler.shutdown ( );       
                          finish ( );
                       }

                     }
                } );
            }
        }, 750, 750, TimeUnit.MILLISECONDS );



回答3:


  • I used this code my problem solved.

  • Try this your problem will be solve. there is no endcall in broadcast so finish() on Idle state. Happy coding!!

    if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
       ((Activity)context).finish();
    }
    


来源:https://stackoverflow.com/questions/14140021/how-to-finish-activity-in-broadcastreceiver-oncallended

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!