我问的原因是因为我是自学代码,而且实现PubNub等服务的时间相当粗糙,因为他们的文档是针对我尚未达到的程序员级别的.
我看一下这些例子并尝试提取最基本的,基本的必需品,但我不确定哪些可以从这些示例类中删除.
感谢有理解我想要问的人.
编辑:要清楚这是我目前的PubNub服务类:
public class PubNubService extends Service {SharedPreferences sP;static final String pub_key = " - ";static final String sub_key = " - ";Pubnub pubnub = new Pubnub(pub_key,sub_key,false);String channel;PowerManager.WakeLock wl = null;private final Handler handler = new Handler() { public voID handleMessage(Message msg) { String pnMsg = msg.obj.toString(); final Toast toast = Toast.makeText(getApplicationContext(),pnMsg,Toast.LENGTH_SHORT); toast.show(); Handler handler = new Handler(); handler.postDelayed(new Runnable() { @OverrIDe public voID run() { toast.cancel(); } },200); }};private voID notifyUser(Object message) { Message msg = handler.obtainMessage(); try { final String obj = (String) message; msg.obj = obj; handler.sendMessage(msg); Log.i("Received msg : ",obj.toString()); } catch (Exception e) { e.printstacktrace(); }}public voID onCreate() { super.onCreate(); Toast.makeText(this,"PubnubService created...",Toast.LENGTH_LONG).show(); PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"SubscribeAtBoot"); if (wl != null) { wl.acquire(); Log.i("PUBNUB","Partial Wake Lock : " + wl.isHeld()); Toast.makeText(this,"Partial Wake Lock : " + wl.isHeld(),Toast.LENGTH_LONG).show(); } Log.i("PUBNUB","PubnubService created..."); try { pubnub.subscribe(new String[] {channel},new Callback() { public voID connectCallback(String channel) { notifyUser("CONNECT on channel:" + channel); } public voID disconnectCallback(String channel) { notifyUser("disCONNECT on channel:" + channel); } public voID reconnectCallback(String channel) { notifyUser("RECONNECT on channel:" + channel); } @OverrIDe public voID successCallback(String channel,Object message) { notifyUser(channel + " " + message.toString()); } @OverrIDe public voID errorCallback(String channel,Object message) { notifyUser(channel + " " + message.toString()); } }); } catch (PubnubException e) { }}@OverrIDepublic voID onDestroy() { super.onDestroy(); if (wl != null) { wl.release(); Log.i("PUBNUB",Toast.LENGTH_LONG).show(); wl = null; } Toast.makeText(this,"PubnubService destroyed...",Toast.LENGTH_LONG).show();}@OverrIDepublic IBinder onBind(Intent intent) { // Todo auto-generated method stub return null;}
上述服务是从this示例复制的.我打电话从我的MainActivity启动此服务.我从我的onCreate方法中这样称呼它:
Intent serviceIntent = new Intent(this,PubNubService.class); startService(serviceIntent);
AndroID Studio对我大吼大叫的一件事是Handler类应该是静态的,否则就会发生泄漏.当我运行我的应用程序时,出现的错误是:[错误:128-0]:无法获取响应代码.请联系支持部门,提供错误详情无法解析主机“pubsub-1.pubnub.com”:没有与主机名关联的地址.在下一行[错误:100-1]:超时发生.
我的AndroID Manifest添加了这个:
<service androID:name=".PubNubService"/>解决方法 PubNub最小的AndroID示例代码发布&订阅
最简单的示例是将所有代码添加到单个Activity中.以下所有代码均可在PubNub Android SDK docs page中看到.
import com.pubnub.API.*;import org.Json.*;Pubnub pubnub = new Pubnub("your_pub_key","your_sub_key");pubnub.subscribe("channel1",new Callback() { @OverrIDe public voID connectCallback(String channel,Object message) { System.out.println("SUBSCRIBE : CONNECT on channel:" + channel + " : " + message.getClass() + " : " + message.toString()); } @OverrIDe public voID disconnectCallback(String channel,Object message) { System.out.println("SUBSCRIBE : disCONNECT on channel:" + channel + " : " + message.getClass() + " : " + message.toString()); } public voID reconnectCallback(String channel,Object message) { System.out.println("SUBSCRIBE : RECONNECT on channel:" + channel + " : " + message.getClass() + " : " + message.toString()); } @OverrIDe public voID successCallback(String channel,Object message) { System.out.println("SUBSCRIBE : " + channel + " : " + message.getClass() + " : " + message.toString()); // this is the messages received from publish // add these messages to a List UI component } @OverrIDe public voID errorCallback(String channel,PubnubError error) { System.out.println("SUBSCRIBE : ERROR on channel " + channel + " : " + error.toString()); } } );
Callback callback = new Callback(){
public voID successCallback(String channel,Object response){
的System.out.println(response.toString());
}
public voID errorCallback(String channel,PubnubError error){
的System.out.println(error.toString());
}
};
pubnub.publish(“my_channel”,“来自PubNub Java SDK的Hello!”,回调);
您可能需要进行一些更改.首先,您应该创建一个click方法,其中包含绑定到界面上按钮的发布.正如subscribe方法的successCallback中所述,您需要在Activity上的UI组件中显示消息.
那应该为你做.
订阅Boot
但实际上没有什么比使用服务将消息作为Intent转发给Activity的Subscribe at Boot sample app更简单了.
防止设备启动时启动服务
启动(启动)设备时启动订阅示例的事实是配置问题.您可以更改清单,以便仅在启动应用程序时启动清单.请参阅SO线程Trying to start a service on boot on Android并撤消使其在引导时启动的部件.
这是有关Android Services的有用信息
关于这个SO线程“Is leaving a pubnub subscription open in a service optimal”的更多细节
总结以上是内存溢出为你收集整理的android – 绝对最小的PubNub服务代码是什么样的?全部内容,希望文章能够帮你解决android – 绝对最小的PubNub服务代码是什么样的?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)