Skip to content

Instantly share code, notes, and snippets.

@psykzz
Created April 2, 2013 20:51
Show Gist options
  • Select an option

  • Save psykzz/5296065 to your computer and use it in GitHub Desktop.

Select an option

Save psykzz/5296065 to your computer and use it in GitHub Desktop.
package com.goon.fleet;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class JabberChatList extends Activity {
@SuppressLint("HandlerLeak")
class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case JabberService.MSG_CLIENT_CONNECTION_CONNECTED:
ping("Jabber connected");
break;
case JabberService.MSG_CLIENT_CONNECTION_DISCONNECTED:
ping("Jabber Disconnected");
break;
case JabberService.MSG_CLIENT_REGISTERED_SUCCEEDED:
ping("Registered Successfully");
break;
case JabberService.MSG_CLIENT_REGISTERED_FAILED:
ping("Registered failed!");
break;
case JabberService.MSG_CLIENT_MESSAGE_RECEIVED:
ping("omg a message :D");
break;
default:
super.handleMessage(msg);
}
}
}
// Logging
private static final String LOGGING_TAG = "JabberChatList";
// Preferences
public static final String PREFS_NAME = "CluserDroid_Prefs";
private SharedPreferences mPrefs;
// Service Bindings
Messenger mService = null;
private boolean mBound = false;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
mService = new Messenger(service);
mBound = true;
JabberService.SendServiceMessage(mService,
JabberService.MSG_JABBER_REGISTER_CLIENT, mMessenger);
}
public void onServiceDisconnected(ComponentName className) {
mService = null;
mBound = false;
}
};
final Messenger mMessenger = new Messenger(new IncomingHandler());
private void doBindService() {
bindService(new Intent(this, JabberService.class), mConnection,
Context.BIND_AUTO_CREATE);
}
private void doUnbindService() {
if (mBound) {
// If we have received the service, and hence registered with it,
// then now is the time to unregister.
if (mService != null) {
JabberService.SendServiceMessage(mService,
JabberService.MSG_JABBER_UNREGISTER_CLIENT);
}
// Detach our existing connection.
unbindService(mConnection);
mBound = false;
}
}
private void loadRecentMessages() {
return; // TO DO
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jabber_chat_list);
// Setup Button Listeners
Button buttonOne = (Button) findViewById(R.id.Button01);
buttonOne.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
JabberService.SendServiceMessage(mService,
JabberService.MSG_JABBER_CONFIG_LOAD);
JabberService.SendServiceMessage(mService,
JabberService.MSG_JABBER_CONNECTION_CONNECT);
}
});
Button buttonTwo = (Button) findViewById(R.id.Button02);
buttonTwo.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
JabberService.SendServiceMessage(mService,
JabberService.MSG_JABBER_CONNECTION_DISCONNECT);
}
});
loadRecentMessages();
}
@Override
protected void onResume() {
super.onStart();
// Bind to the service
doBindService();
}
@Override
protected void onStart() {
super.onStart();
// Bind to the service
doBindService();
}
@Override
protected void onStop() {
super.onStop();
// Unbind from the service
doUnbindService();
}
public void ping(String msg) {
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment