Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7882487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:17:47+00:00 2026-06-03T04:17:47+00:00

So i have a service for an application that i am working on. This

  • 0

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
        }
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T04:17:49+00:00Added an answer on June 3, 2026 at 4:17 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Web Service and an Android application that uses this web service.
I have a legacy Windows server service and (spawned) application that works fine in
The application that I am working on is a Service Desk App. I have
I am working on an application that runs a service. This service starts automatically
I have a Java application that is working as a service. After several days
I have been working on a BlackBerry application that consumes web services from ColdFusion
I have an service application that reads a config file containing a list of
We have a java web service application that uses log4j to do logging. An
enter code here Hi All, I have a simple windows service application that connects
I have a silverlight/RIA service application and in that, after I save the data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.