问题
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 sofinish()
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