Skip to content

Instantly share code, notes, and snippets.

@zaur
Created October 22, 2015 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zaur/6be543b4f395444f52fd to your computer and use it in GitHub Desktop.
Save zaur/6be543b4f395444f52fd to your computer and use it in GitHub Desktop.
package ru.agamov.mqttsample;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.pubnub.api.Callback;
import com.pubnub.api.Pubnub;
import com.pubnub.api.PubnubError;
import com.pubnub.api.PubnubException;
public class MainActivity extends AppCompatActivity {
Pubnub pubnub;
private static final String TAG = "PubNub";
WifiReceiver mReceiver;
IntentFilter mFilter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupReceiver();
setupPubNub();
}
@Override
protected void onPause() {
unregisterReceiver(mReceiver);
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
registerReceiver(mReceiver, mFilter);
}
private void setupReceiver() {
mReceiver = new WifiReceiver();
mFilter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
}
private void setupPubNub() {
pubnub = new Pubnub("pub-c-19bcd7c5-0d5c-4159-9919-7f79d4944c56", "sub-c-6b78c260-7880-11e5-a49d-02ee2ddab7fe");
// final Pubnub pubnub = new Pubnub("demo", "demo");
try {
pubnub.subscribe("my_channel", new Callback() {
@Override
public void connectCallback(String channel, Object message) {
pubnub.publish("my_channel", "Hello from the PubNub Java SDK", new Callback() {
});
}
@Override
public void disconnectCallback(String channel, Object message) {
Log.d(TAG, "SUBSCRIBE : DISCONNECT on channel:" + channel
+ " : " + message.getClass() + " : "
+ message.toString());
}
public void reconnectCallback(String channel, Object message) {
Log.d(TAG, "SUBSCRIBE : RECONNECT on channel:" + channel
+ " : " + message.getClass() + " : "
+ message.toString());
}
@Override
public void successCallback(String channel, Object message) {
Log.d(TAG, "SUBSCRIBE : " + channel + " : "
+ message.getClass() + " : " + message.toString());
}
@Override
public void errorCallback(String channel, PubnubError error) {
Log.e(TAG, "SUBSCRIBE : ERROR on channel " + channel
+ " : " + error.toString());
}
}
);
} catch (PubnubException e) {
Log.e(TAG, "Exception", e);
//System.out.println(e.toString());
}
}
public class WifiReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMan.getActiveNetworkInfo();
if (netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_WIFI)
Log.d("__WifiReceiver__", "Connected!");
else
Log.d("__WifiReceiver__", "Disconnected!!!");
}
}
;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment