我正致力于将基于Quickblox的视频聊天整合到我的应用中。 在这里,我在前台服务中运行一个监听器,监听对手发起的视频通话。这里在switch语句中,在呼叫接受状态,我正在调用一个新的活动,这只是一个带有接受和断开按钮的拨号屏幕。接受按钮连接呼叫,但断开连接切断呼叫并关闭活动,但多次相同的活动(拨号器屏幕)重复打开,我的问题是如何使服务呼叫一次,如果其断开连接忽略其余的呼叫。 (我向对手发送拒绝信号,因此从对手那里呼叫掉线但是这里的服务重复呼叫活动虽然呼叫从对手中掉线)
这是代码 带有switch语句的OnQBVideoChatListener的VchatListener服务具有调用新活动的接受调用状态
public class VChatListener extends Service {
private VideoChatConfig videoChatConfig;
private CallState state;
QBUser a=DataHolder.getInstance().getCurrentQbUser();
/* private AlertDialog alertDialog;*/
private static final int notif_id=1;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
this.startForeground();
}
private void startForeground() {
startForeground(notif_id, getMyActivityNotification(""));
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
//startForeground(FOREGROUND_ID, notification);
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
Log.i("******Service Call user Here","now*******");
if(a!=null){
Log.i("******Service testing",""+a);
}else{
Log.i("******Service testing user","nothing");
}
Log.i("***Still","running****");
QBVideoChatController.getInstance().setQBVideoChatListener(a, qbVideoChatListener);
} catch (NullPointerException ex) {
ex.printStackTrace();
} catch (XMPPException e) {
e.printStackTrace();
}
};
};
new Thread(runnable).start();
return Service.START_REDELIVER_INTENT;
private OnQBVideoChatListener qbVideoChatListener = new OnQBVideoChatListener() {
@Override
public void onVideoChatStateChange(CallState state, VideoChatConfig receivedVideoChatConfig) {
Debugger.logConnection("onVideoChatStateChange: " + state);
videoChatConfig = receivedVideoChatConfig;
/*if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}*/
Log.i("service state",""+state.toString());
switch (state) {
case ACCEPT:
Intent intent = new Intent(VChatListener.this, CallActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(VideoChatConfig.class.getCanonicalName(), videoChatConfig);
startActivity(intent);
// showCallDialog();
Log.i("service state 1",""+state.toString());
break;
case ON_ACCEPT_BY_USER:
Log.i("service state 2",""+state.toString());
/* QBVideoChatController.getInstance().onAcceptFriendCall(videoChatConfig, null);*/
//startVideoChatActivity();
break;
case ON_REJECTED_BY_USER:
Log.i("service state 3",""+state.toString());
/*Toast.makeText(VChatListener.this, "Rejected by user", Toast.LENGTH_SHORT).show();*/
break;
case ON_DID_NOT_ANSWERED:
Log.i("service state 4",""+state.toString());
/* Toast.makeText(VChatListener.this, "User did not answer", Toast.LENGTH_SHORT).show();*/
break;
case ON_CANCELED_CALL:
videoChatConfig = null;
Log.i("service state 5","nothing");
/*if (alertDialog != null && alertDialog.isShowing()){
alertDialog.dismiss();
}
autoCancelHandler.removeCallbacks(autoCancelTask);*/
break;
}
}
};
public void onDestroyed(){
super.onDestroy();
}
private Notification getMyActivityNotification(String text){
// The PendingIntent to launch our activity if the user selects
// this notification
CharSequence title = getText(R.string.title_activity);
/* PendingIntent contentIntent = PendingIntent.getActivity(this,0, new Intent(this,Appointment.class), 0);*/
return new Notification.Builder(this)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.drawable.ic_launcher)
.getNotification();
}
}
这是活动将通过服务调用与功能结束相关联的结束按钮,断开呼叫。
public class CallActivity extends Activity {
private ProgressDialog progressDialog;
private VideoChatConfig videoChatConfig;
private VChatListener vChatListener;
QBUser qbuser;
MediaPlayer mMediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_call);
ImageButton b1 = (ImageButton) findViewById(R.id.button1);
ImageButton b2 = (ImageButton) findViewById(R.id.button2);
playRingtone();
/*while(!b1.isPressed() || !b2.isPressed()){
playRingtone();
}*/
progressDialog = new ProgressDialog(this);
progressDialog.setMessage(getString(R.string.please_wait));
progressDialog
.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
// TODO add stopCalling here, send Cancel message if
// need
// XMPPSender.sendCancelCallMsg(videoChatConfig);
// QBVideoChatController.getInstance().stopCalling();
}
});
qbuser = DataHolder.getInstance().getCurrentQbUser();
videoChatConfig = getIntent().getParcelableExtra(
VideoChatConfig.class.getCanonicalName());
;
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onAcceptCallClick();
mMediaPlayer.stop();
}
private void onAcceptCallClick() {
//mMediaPlayer.stop();
QBVideoChatController.getInstance().acceptCallByFriend(
videoChatConfig, null);
startVideoChatActivity();
}
private void startVideoChatActivity() {
if (videoChatConfig.getCallType() == CallType.VIDEO_AUDIO) {
Intent intent = new Intent(getBaseContext(),
ActivityVideoChat.class);
intent.putExtra(VideoChatConfig.class.getCanonicalName(),
videoChatConfig);
startActivity(intent);
} else if(videoChatConfig.getCallType() == CallType.AUDIO) {
Intent intent = new Intent(getBaseContext(),
ActivityAudioChat.class);
intent.putExtra(VideoChatConfig.class.getCanonicalName(),
videoChatConfig);
startActivity(intent);
}
}
});
}
public void end(View view)
{
mMediaPlayer.stop();
QBVideoChatController.getInstance().finishVideoChat(videoChatConfig);
XMPPSender.sendCancelCallMsg(videoChatConfig);
QBVideoChatController.getInstance().stopCalling();
super.onDestroy();
finish();
}
public void playRingtone() {
try {
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(this, alert);
final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_RING) != 0) {
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
mMediaPlayer.setLooping(true);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
} catch(Exception e) {
e.printStackTrace();
}
/* try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(),
notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}*/
}
}
一旦我包含在清单启动模式=“singleTask”中一次启动活动,将服务限制为调用活动,但是当通过断开连接活动时,它再次被调用并且不会停止。
<activity
android:name="com.yugays.videochatsample.ui.CallActivity"
android:label="@string/app_name"
android:launchMode="singleTask"
android:screenOrientation="landscape" >
</activity>
如何在呼叫断开且活动关闭后停止创建活动。