0
我以前使用過後臺服務中的PubNub在Android上發佈位置更新。我希望將它用於不同的項目,並編寫了一個簡單的服務來測試新的API。然而,出版總是失敗,我無法明確原因。PubNub未從服務發佈
這裏的服務代碼:
public class MessageService extends Service {
private static final String TAG = "MessageService";
private PubNub pubnub;
private Timer timer = new Timer();
public MessageService() {
PNConfiguration config = new PNConfiguration();
config.setPublishKey("pub_key_removed_for_privacy");
pubnub = new PubNub(config);
}
TimerTask task = new TimerTask() {
@Override
public void run() {
pubnub.publish()
.channel("demo")
.message("hello from service")
.async(new PNCallback<PNPublishResult>() {
@Override
public void onResponse(PNPublishResult result, PNStatus status) {
if (status.isError()) {
Log.e(TAG, "Publish failed");
} else {
Log.d(TAG, "Publish successful");
}
}
});
}
};
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
timer.schedule(task, 0, 5000);
return START_REDELIVER_INTENT;
}
@Override
public void onDestroy() {
super.onDestroy();
timer.cancel();
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
private final IBinder binder = new ServiceBinder();
public class ServiceBinder extends Binder {
public MessageService getService() {
return MessageService.this;
}
}
}
在MainActivity,我只需調用startService(),看日誌,我總是得到故障信息。我在我的清單中擁有internet和network_state權限,因此無法考慮其他原因導致無法使用的原因。有什麼建議麼?
@CraigConover你可以發表這個答案,所以我可以把它標記爲這樣嗎? –