gpt4 book ai didi

android - 绝对最小的 PubNub 服务代码是什么样的?

转载 作者:太空宇宙 更新时间:2023-11-03 11:04:52 25 4
gpt4 key购买 nike

在服务类中建立 PubNub 订阅所需的最低代码是多少? PubNub 上的示例包括启动订阅、广播接收器、pushalarms 等的代码。我是否相信所有 this来自 github 的代码是最低要求吗?

我问的原因是因为我是自学代码并且在实现诸如 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) {

}
}

@Override
public void onDestroy() {
super.onDestroy();
if (wl != null) {
wl.release();
Log.i("PUBNUB", "Partial Wake Lock : " + wl.isHeld());
Toast.makeText(this, "Partial Wake Lock : " + wl.isHeld(), Toast.LENGTH_LONG).show();
wl = null;
}
Toast.makeText(this, "PubnubService destroyed...", Toast.LENGTH_LONG).show();
}

@Override
public 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 类应该是静态的,否则会发生泄漏。当我运行我的应用程序时,发生的错误是:[Error: 128-0] : Unable to get Response Code。请联系支持人员并提供错误详细信息。无法解析主机“pubsub-1.pubnub.com”:没有与主机名关联的地址。在下一行[Error: 100-1] : Timeout Occurred

我的 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 = new Callback() { public void successCallback(String channel, Object response) { System.out.println(response.toString()); } public void errorCallback(字符串 channel ,PubnubError 错误){ System.out.println(error.toString()); }};pubnub.publish("my_channel", "来自 PubNub Java SDK 的问候!", 回调);

您可能需要进行一些更改。首先,您应该创建一个点击方法,其中包含绑定(bind)到界面上按钮的发布。正如订阅方法的 successCallback 中所述,您需要在 Activity 的 UI 组件中显示消息。

这应该为您完成。

开机订阅

但是没有比我们的 Subscribe at Boot sample app 更简单的了使用 Service 将消息作为 Intent 转发到 Activity。

防止服务在设备启动时启动

启动时订阅示例在设备启动(开机)时启动这一事实是一个配置问题。您可以更改 list ,使其仅在应用程序启动时启动。查看 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 服务代码是什么样的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35006946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com