- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在服务类中建立 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"/>
最佳答案
最简单的示例是将所有代码添加到单个 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/
我想要做的是在每个框的蓝色标题之后获取红色文本。 看来我必须添加另一个 div?我已经添加并修改了 .card-indus img 的位置,但结果永远不是我想要的。 你知道为什么即使我将 positi
所以我一直在努力掌握绝对和相对定位的诀窍。关于这个主题有很多教程和问题,我已尽力理解它们。除了这一点,我大部分时间都很好。我正在创建一个页面,其中有较小的缩略图图像,用户可以选择单击并展开图像。为此,
下面是我正在处理的代码。如您所见,有一个“#parent”div 和一个“#child”div。 '#child' div 有一个未定义的高度,这是因为有时,'#child' 的高度小于或长于它的父级
我目前正在创建一个看起来有点像这样的固定 header 。 https://gyazo.com/e0bab8ba195e33110b19123c7fc3c568 Logo 始终位于左侧,小按钮始终位于
我怎样才能得到一个绝对定位的子 div,当它放在父 div 的范围之外时不显示? https://jsfiddle.net/knp9ebys/9/ .papa { background:red;
如果我对 CSS 显得相当“菜鸟”,我深表歉意。我一直在尝试设置以下... #0 { width: 100%; height: y; border: 1px solid black; } #
很长一段时间以来,我一直在摆弄一个特定的布局问题,但我显然采用了错误的方式。 以下是分解为基本组成部分的方法: SOME HEADER
我创建了几个虚拟 Controller ,我希望能够从当前的 http 请求中获取 url。 例如: http://www.site.com/app_1/default.aspx ===> http:
我创建了几个虚拟 Controller ,我希望能够从当前的 http 请求中获取 url。 例如: http://www.site.com/app_1/default.aspx ===> http:
我想知道是否有一个库在某处提供受新类型保护的 FilePath 类型。我找到了我想要的http://hackage.haskell.org/package/darcs-2.8.4/docs/src/D
如果我尝试使用以下方式连接到我的嵌入式数据库: private static String url = "jdbc:sqlite:~/hr4413/pkg/sqlite/Models_R_US.db"
所以我是 django 的新手,我一直在研究 PHP CodeIgniter,其中将绝对 URL 放入 href 我通过调用 URL 帮助程序使用了一个名为 base_url 的函数 它给出的输出类
我有一个小问题。 我在其他 div 中有一些图像元素的容器 div。像这样的东西: 我需要将容器垂直居中,但我不能使用顶部:-healfHeight; mar
我有一个带有 inline-block css 位置的列表(div)。里面有一个 relative 定位的 ul 是隐藏的。所以我试图通过添加一些类将这个 div 转换为 absolute 。通常,当
我正在尝试设置一个卡住列,唯一需要解决的问题是同一行上其他 td 的高度不会扩展以匹配绝对定位 td 的高度。由于卡住标题中的文本是任意的,它可以跨越多行。 如果它不是绝对定位的,那么这将迫使同一行中
这个问题在这里已经有了答案: Centering text vertically and horizontally in a div (1 个回答) 关闭 5 年前。
当它的位置绝对时,我试图使一个框宽度为 100%? 下图是我想要做的 https://i.imgur.com/qMaT361.gif float .box1 { position:re
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and t
我有 3 个 div,都是 positioned: absolute,但是我想要填充窗口宽度的 div 只会适应其中文本的长度。我希望黄色 div #help 填充窗口的其余部分。 我知道这听起来很菜
这个问题在这里已经有了答案: Retrieve the position (X,Y) of an HTML element (32 个答案) 关闭 8 年前。 有时候,当我请求某个对象的.posit
我是一名优秀的程序员,十分优秀!