Service pauses on screen lock

ⅰ亾dé卋堺 提交于 2019-12-02 12:43:56

问题


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, !


回答1:


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




回答2:


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.




回答3:


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);



来源:https://stackoverflow.com/questions/9309044/service-pauses-on-screen-lock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!