java - Android Service for PubNub -


i have implemented pubnub subscribe , publish code. code working fine on activity. want execute code in background of service class. have created class extending intentservice. , subscribing pubnub channel in oncreate method. whenever running app service stopping without showing pubnub status. getting following pubnub error. have linked pubnub required libraries too.

04-09 23:39:32.621: d/service message(10033): error[error: 100-1] : timeout occurred 

mainactivity.java

public class mainactivity extends activity {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);     }      public void startservice(view v){         startservice(new intent(this, myservice.class));     }      public void stopservice(view v){         stopservice(new intent(this, myservice.class));     }   } 

pubnubhandler.java

public class pubnubhandler{      public static final string global_channel = "my_channel_name";     public static final string publish_key =              "my_publish_key";     public static final string subscribe_key =              "my_subscribe_key";     private context context;     private pubnub pubnub;       public pubnubhandler(context context) {          this.context = context;         pubnub = new pubnub(publish_key, subscribe_key);         pubnub.setretryinterval(1000);     }      public void notifyuser(string message) {          final string msg = message;         handler handler = new handler(looper.getmainlooper());          handler.post(new runnable() {              @override             public void run() {                  toast.maketext(context, msg, 0).show();              }         });        }      public void subscribe() {          callback callback = new callback() {             @override             public void connectcallback(string channel, object message) {                 log.d("service message", "subscribed");             }              @override             public void disconnectcallback(string channel, object message) {                 log.d("service message", "disconnected");             }              public void reconnectcallback(string channel, object message) {                 log.d("service message", "reconnected");             }              @override             public void successcallback(string channel, final object message) {                 log.d("service message", "message : "+message.tostring());             }              @override             public void errorcallback(string channel, pubnuberror error) {                 log.d("service message", "error"+error.tostring());             }         };          try {             pubnub.subscribe(global_channel, callback);         } catch (pubnubexception e) {             system.out.println(e.tostring());         }     }      public void unsubscribe() {         pubnub.unsubscribe(global_channel);     }      public void publish(string message) {          callback callback = new callback() {             public void successcallback(string channel, object response) {              }             public void errorcallback(string channel, pubnuberror error) {                  notifyuser("something went wrong. try again.");             }         };         pubnub.publish(global_channel, message , callback);       }  } 

myservice.java

public class myservice extends intentservice {      public myservice() {         super("my service");         // todo auto-generated constructor stub     }      @override     public void oncreate() {         super.oncreate();         toast.maketext(this, "service created", 1).show();         new pubnubhandler(this).subscribe();     }      @override     public int onstartcommand(intent intent, int flags, int startid) {         return super.onstartcommand(intent, flags, startid);     }      @override     public void ondestroy() {         super.ondestroy();         toast.maketext(this, "service destroyed", 1).show();     }      @override     protected void onhandleintent(intent arg0) {      } } 

manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.example.servicedemo"     android:versioncode="1"     android:versionname="1.0" >      <uses-sdk         android:minsdkversion="16"         android:targetsdkversion="17" />      <uses-permission android:name="android.permission.internet" />     <uses-permission android:name="android.permission.access_network_state" />      <application         android:allowbackup="true"         android:icon="@drawable/ic_launcher"         android:label="@string/app_name"         android:theme="@style/apptheme" >         <activity             android:name=".mainactivity"             android:label="@string/app_name" >             <intent-filter>                 <action android:name="android.intent.action.main" />                  <category android:name="android.intent.category.launcher" />             </intent-filter>         </activity>          <service android:name=".myservice" >         </service>     </application>  </manifest> 

you should work in intentservice's onhandleintent(), not in oncreate(). reason because intentservice stops haven't supplied code onhandleintent(). intentservice shuts down when onhandleintent() finishes , there weren't other startservice() calls.

in case, though, calls pubnub api asynchronous , therefore happen in background. maybe don't need intentservice here @ all. if want create model object keep data survive activity's configuration changes, consider using headless fragment or plain service.


Popular posts from this blog