I have implemented PubNub subscribe and publish code. My code is working fine on activity. But now I want to execute that code in background with the help of service class. I have created my class extending the IntentService
. And I am subscribing to pubnub channel in onCreate
method. But whenever I am running the app service is immediately stopping without showing pubnub status. I am getting following pubnub error. I 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 always do the work in IntentService
's onHandleIntent()
, not in onCreate()
. The reason because your IntentService
stops immediately is you haven't supplied any code to onHandleIntent()
. IntentService
always shuts down itself when onHandleIntent()
finishes and there weren't any other startService()
calls.
In your case, though, calls to PubNub
API are asynchronous and therefore happen already in background. Maybe you don't need IntentService
here at all. If you want to create a model object to keep data that will survive Activity
's configuration changes, consider using headless Fragment or plain Service
.
Try to extend MyService
from simple Service
instead of IntentService
. IntentService
calls stopSelf()
just after the last task was completed. This means that there would be no Context
associated with PubnubHandler
and the system can just kill a Thread
where subscribe stuff is running.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With