So i have a service for an application that i am working on. This service contacts my server every 20 seconds to see if there is an update and serves a notification if there is. I am running into a few problems here.
1) My service tends to die after 30 or so mins. it looks like android is just killing it. how can I keep it alive all the time?
2) as you can see in my code, my wakelock is weird. i need to keep it alive during sleep to check for notifications, but if i acquire a wakelock after waiting 20 seconds it doesn't work. so right now it releases and then re-acquire the wakelock before the timer. which isnt good for battery and such. anyone know a better way do do this?
import java.io.IOException;
import java.net.UnknownHostException;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
public class UpdateService extends Service {
private boolean isRunning = true;
String username, pass, outpt;
String ns = Context.NOTIFICATION_SERVICE;
int Notify_ID = 0;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
username = intent.getStringExtra("username");
pass = intent.getStringExtra("pass");
isRunning = true;
Thread contact = new Thread(new Contact());
contact.start();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
// Stop the Background thread
isRunning = false;
}
private class Contact implements Runnable {
public void run() {
while (isRunning) {
PowerManager mgr = (PowerManager) UpdateService.this
.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyWakeLock");
wakeLock.acquire();
try {
Thread.currentThread().sleep(20000);
ServerContact server = new ServerContact("stuff i send to the server");
outpt = server.connect();
if (!outpt.equals("false")) {
handler.sendEmptyMessage(0);
}
} catch (UnknownHostException e) {
System.err.println("cannot connect to host.");
} catch (IOException e) {
System.err.println("Server Contact ERROR!! ");
} catch (InterruptedException e) {
System.err.println("thread sleep interrupt ");
}
wakeLock.release();
}
}
public Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0) {
for (String item : outpt.split("\\s+")) {
Notify_ID++;
notifySys(item, Notify_ID);
}
}
}
};
}
public void notifySys(String data, int id) {
// id/name/location
try {
String[] content = data.split("/");
int icon = R.drawable.wcicon;
CharSequence tickerText = "Word Circle";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = content[2];
CharSequence contentText = "It is your turn!";
Intent go = new Intent(UpdateService.this, SomeClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(
UpdateService.this, 0, go, 0);
notification.setLatestEventInfo(context, contentTitle, contentText,
contentIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
notification.defaults = Notification.DEFAULT_ALL;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(Notify_ID, notification);
} catch (Exception e) {
// TODO: handle exception
}
}
}
What you are doing is known as Polling but you need to do a Push notification using C2DM. You can watch this video at Google IO 2010. You can get sample code for C2DM here.