1

For testing purposes i have made a service that beeps every 1 minute. (No client-server interface yet). It beeps okay when the screen in on, but when it goes to sleep the beeping stops.

I am making an application that has to periodically poll the a server for something.

For this, I am trying to create a service that'll constantly be running in the background, poll the server every 1 min and then based on the reply from server it shall generate a task bar notification.

I have a test activity with two buttons, 1 to start and the other to stop the service. And one service class named S_PS_PollService

The setOnClickListener of 'Start Activity' button contains:

Thread pollServiceThread = new Thread() {
  public void run() {
    startService(new Intent(MM_MainMenu.this,
    S_PS_PollService.class));
  }
};

pollServiceThread.start();

The 'Stop Activity' button simply has:

stopService(new Intent(MM_MainMenu.this, S_PS_PollService.class));

Following are the methods from S_PS_PollService class:

public void onCreate() {
 pollSound = MediaPlayer.create(this, R.raw.chirp);
 alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
 Intent myIntent = new Intent(this, S_PS_PollService.class);
 pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
 // for wake lock
 pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
 wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag")
 // for calendar
 calendar = Calendar.getInstance();
}

Onstart:

public void onStart(Intent intent, int startId) {
 super.onStart(intent, startId);

 wl.acquire();

 pollSound.start();

 calendar.setTimeInMillis(System.currentTimeMillis());
 calendar.add(Calendar.MILLISECOND, 60000);
 alarmManager.set(AlarmManager.RTC_WAKEUP,
 calendar.getTimeInMillis(), pendingIntent);

 wl.release();
}

Whenever the alarm kicks off onStart() method is executed, making the beep and setting new alarm. But it works only as long as screen is on.

I have tried for https://github.com/commonsguy/cwac-wakeful but didnt get it. Relatively new to android ...

Please help me, im very desperate :) Thanks, !

saad
  • 475
  • 7
  • 11

3 Answers3

0

You have to use the AlarmManager, there are plenty of posts here on stackoverflow.

svenmeier
  • 5,681
  • 17
  • 22
0

You want to acquire a partial wake lock (leaving the CPU running whenever sleep is entered on the device) as suggested by your code.

The issue is your presumably overriden on start releases the wake lock. You want to release your wakeLock in onDestroy .. once your service is finished running.

Tim Capes
  • 655
  • 2
  • 7
  • 12
0

This finally worked for me. Download the CWAC-WakefulIntentService.jar from https://github.com/commonsguy/cwac-wakeful

add a class in your project

import com.commonsware.cwac.wakeful.WakefulIntentService;

public class WakeService extends WakefulIntentService {

    public WakeService(String name) {

        super(name);

    }
    @Override
    protected void doWakefulWork(Intent intent) {
    }
}

now add the following line in your code where ever you want to repeat the loop and wake the device up

WakefulIntentService.sendWakefulWork(this, S_WS_WakeService.class);

Sunil D.
  • 17,983
  • 6
  • 53
  • 65
saad
  • 475
  • 7
  • 11