【发布时间】:2019-05-13 13:32:00
【问题描述】:
每当用户选择一个按钮时,我都会使用后台服务来播放音频。但我想做的是每 60 分钟播放一次音频。我该怎么做呢?
我已经尝试使用处理程序并将计时器设置为 60 分钟,然后执行播放音频的代码行,但只要用户选择按钮,它就会自动播放音频。
public class BackgroundService extends Service {
private MediaPlayer player;
private Boolean state = false;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//i would like to play this audio every 60minute in the background
player = MediaPlayer.create(BackgroundService.this, Settings.System.DEFAULT_RINGTONE_URI);
//this will make the ringtone continuously playing
player.setLooping(true);
//staring the player
player.start();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
//stopping the player when service is destroyed
player.stop();
}
}
【问题讨论】:
-
请添加更多详细信息,例如您当前如何使用该服务以了解情况。
-
您是否检查过以下链接:stackoverflow.com/questions/16460612/…