Back to project page opentok-android-sdk-samples.
The source code is released under:
Apache License
If you think the Android project opentok-android-sdk-samples listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.opentok.android.demo.opentoksamples; //w w w . ja v a2 s . co m import android.app.ActionBar; import android.app.Activity; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.media.AudioManager; import android.os.Bundle; import android.os.IBinder; import android.support.v4.app.NotificationCompat; import android.support.v4.view.ViewPager; import android.util.Log; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import android.widget.ScrollView; import android.widget.TextView; import com.opentok.android.demo.services.ClearNotificationService; import com.opentok.android.demo.services.ClearNotificationService.ClearBinder; import com.opentok.android.demo.multiparty.MySession; import com.opentok.android.demo.opentoksamples.R; public class MultipartyActivity extends Activity { private static final String LOGTAG = "demo-subclassing"; private MySession mSession; EditText mMessageEditText; private boolean resumeHasRun = false; private boolean mIsBound = false; private NotificationCompat.Builder mNotifyBuilder; NotificationManager mNotificationManager; ServiceConnection mConnection; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.room); ActionBar actionBar = getActionBar(); actionBar.setHomeButtonEnabled(true); actionBar.setDisplayHomeAsUpEnabled(true); mSession = new MySession(this); mMessageEditText = (EditText) findViewById(R.id.message); ViewGroup preview = (ViewGroup) findViewById(R.id.preview); mSession.setPreviewView(preview); mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); ViewPager playersView = (ViewPager)findViewById(R.id.pager); mSession.setPlayersViewContainer(playersView); mSession.setMessageView((TextView)findViewById(R.id.messageView), (ScrollView)findViewById(R.id.scroller)); mSession.connect(); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: onBackPressed(); return true; default: return super.onOptionsItemSelected(item); } } @Override public void onPause() { super.onPause(); if ( mSession != null ){ mSession.onPause(); } mNotifyBuilder = new NotificationCompat.Builder(this) .setContentTitle(this.getTitle()) .setContentText(getResources().getString(R.string.notification)) .setSmallIcon(R.drawable.ic_launcher).setOngoing(true); Intent notificationIntent = new Intent(this, MultipartyActivity.class); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent intent = PendingIntent.getActivity(this, 0, notificationIntent, 0); mNotifyBuilder.setContentIntent(intent); if(mConnection == null){ mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName className,IBinder binder){ ((ClearBinder) binder).service.startService(new Intent(MultipartyActivity.this, ClearNotificationService.class)); NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); mNotificationManager.notify(ClearNotificationService.NOTIFICATION_ID, mNotifyBuilder.build()); } @Override public void onServiceDisconnected(ComponentName className) { mConnection = null; } }; } if(!mIsBound){ bindService(new Intent(MultipartyActivity.this, ClearNotificationService.class), mConnection, Context.BIND_AUTO_CREATE); mIsBound = true; } } @Override public void onResume() { super.onResume(); if(mIsBound){ unbindService(mConnection); mIsBound = false; } if (!resumeHasRun) { resumeHasRun = true; return; } else { if ( mSession != null ){ mSession.onResume(); } } mNotificationManager.cancel(ClearNotificationService.NOTIFICATION_ID); } @Override public void onStop() { super.onStop(); if(mIsBound){ unbindService(mConnection); mIsBound = false; } if (isFinishing()) { mNotificationManager.cancel(ClearNotificationService.NOTIFICATION_ID); if (mSession != null) { mSession.disconnect(); } } } @Override public void onDestroy() { mNotificationManager.cancel(ClearNotificationService.NOTIFICATION_ID); if(mIsBound){ unbindService(mConnection); mIsBound = false; } if (mSession != null) { mSession.disconnect(); } restartAudioMode(); super.onDestroy(); finish(); } @Override public void onBackPressed() { if (mSession != null) { mSession.disconnect(); } restartAudioMode(); super.onBackPressed(); } public void onClickSend(View v) { if(mMessageEditText.getText().toString().compareTo("") == 0){ Log.i(LOGTAG, "Cannot Send - Empty String Message"); } else{ Log.i(LOGTAG, "Sending a chat message"); mSession.sendChatMessage(mMessageEditText.getText().toString()); mMessageEditText.setText(""); } } public void restartAudioMode() { AudioManager Audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE); Audio.setMode(AudioManager.MODE_NORMAL); this.setVolumeControlStream(AudioManager.USE_DEFAULT_STREAM_TYPE); } }