Back to project page PubNubTest.
The source code is released under:
GNU General Public License
If you think the Android project PubNubTest listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
// Copyrights Fahad Zafar 2013 // Email: zoalord12@gmail.com // www .java 2 s. c o m package com.example.pubnubtest; import java.util.Hashtable; import com.pubnub.api.Callback; import com.pubnub.api.Pubnub; import com.pubnub.api.PubnubError; import android.util.Log; // This class performs the controller tasks. The purpose is to initialize PubNub // and perform the subscribe and publish duties. public class PubManager { // String tokens generated by PubNub. final String kPubKey = "pub-c-3aca05cd-54bb-4062-80de-346e5c804116"; final String kSubKey = "sub-c-1224b9dc-5238-11e3-8615-02ee2ddab7fe"; // Name of the channel on the account used for this demo. final String kChannel = "my_channel"; // The pubNub object. Pubnub pubnub; // The caller initializing the object automatically subscribes to the // channel. public PubManager() { pubnub = new Pubnub(kPubKey, kSubKey, "", false); subscribe(); } // The subscribe functionality. public void subscribe() { // Populate the data elements. Hashtable args = new Hashtable(1); args.put("channel", kChannel); try { pubnub.subscribe(args, new Callback() { @Override public void connectCallback(String channel, Object message) { Log.w("Sucess", " Subscribed ....."); } @Override public void disconnectCallback(String channel, Object message) { } @Override public void reconnectCallback(String channel, Object message) { } @Override public void successCallback(String channel, Object message) { // The message is returned and using the length of the // message we can identify which one has been updated to the // subscriber. String data = message.toString(); int index = data.length() / 250 - 1; long currentTime = Benchmark.GetTimeDifference(index); MainActivity.VALUES[index] = currentTime; MainActivity.UpdateChart(index); } @Override public void errorCallback(String channel, PubnubError error) { } }); } catch (Exception e) { Log.w("PubManager","Error in subscribe:" + e.getMessage()); } } public void publish(String data) { try { Hashtable args = new Hashtable(2); args.put("message", data); args.put("channel", kChannel); pubnub.publish(args, new Callback() { @Override public void successCallback(String channel, Object message) { } @Override public void errorCallback(String channel, PubnubError error) { Log.w("PubManager","Error:" + error.getErrorString()); // If any of the measurements fail, just reset the whole // benchmark for now. Benchmark.ResetAll(); } }); } catch (Exception e) { Log.w("PubManager", "Error in publish"); e.printStackTrace(); } } }