private class OmegleChat extends AsyncTask { @Override protected void onPreExecute() { } @Override protected void onProgressUpdate(Void... values) { } @Override protected Void doInBackground(Void... arg0) { // TODO Auto-generated method stub runOnUiThread(new Runnable() { @Override public void run() { OmgeleChatOn(); if(mConnection.isConnected()) { mConnection.disconnect(); } } }); return null; } @Override protected void onPostExecute(Void result) { } } public void OmgeleChatOn() { Omegle omegle = new Omegle(); try { System.out.println("Opening session..."); session = omegle.openSession(OmegleMode.NORMAL, new OmegleEventAdaptor() { @Override public void chatWaiting(OmegleSession session) { System.out.println("Waiting for chat..."); } @Override public void chatConnected(OmegleSession session) { System.out.println("You are now talking to a random stranger!"); omgcheck=1; runOnUiThread(new Runnable() { @Override public void run() { tv=(TextView)findViewById(R.id.txtMessagesReceived2); tv.setText("Talk"); ibutton.setEnabled(false); dis=(TextView)findViewById(R.id.distance); dis.setText("Distance: Not Available"); if(gpchk==1) { lm.removeUpdates(ll); } } }); } @Override public void chatMessage(OmegleSession session, final String message) { System.out.println("Stranger: " + message); runOnUiThread(new Runnable() { @Override public void run() { messages.add(new Message(true,message,false)); adapter.notifyDataSetChanged(); getListView().setSelection(messages.size()-1); } }); } @Override public void messageSent(OmegleSession session, final String string) { System.out.println("You: " + string); /*runOnUiThread(new Runnable() { @Override public void run() { txt.append("You:"+string+"\n"); } });*/ } @Override public void strangerDisconnected(OmegleSession session) { System.out.println("Stranger disconnected, goodbye!"); omgcheck=0; runOnUiThread(new Runnable() { @Override public void run() { clearList(); start(); } }); //System.exit(0); } @Override public void omegleError(OmegleSession session, String string) { System.out.println("ERROR! " + string); //System.exit(1); } }); } catch (OmegleException e) { e.printStackTrace(); } }