【发布时间】:2015-07-17 20:27:35
【问题描述】:
我正在尝试创建一个警报,其中包含我已经存储在 Parse 中的时间列表。到目前为止,我可以让闹钟响起。但现在我希望它即使在设备处于睡眠状态时也能关闭。我知道我需要在我已经做过的服务中实现唤醒锁。我也明白我需要包含“FLAG_SHOW_WHEN_LOCKED | FLAG_TURN_SCREEN_ON, FLAG_FULLSCREEN”。目前我知道的唯一方法是使用 get Window。但我知道 getWindow 不是为了服务。所以,我需要一些帮助来解决这个问题。
我有一个带有此代码的 AlarmService:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Toast.makeText(this,"onStartCommand()",Toast.LENGTH_SHORT).show();
//wake lock
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, "My Wake Log");
mWakeLock.acquire();
//This code below doesn't work because getWindow is not for Service. what is my other option?
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
setContentView(R.layout.activity_reminder_screen);
//start alarm screen
Intent intent = new Intent(this, AlarmScreenActivity.class);
ReminderIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
return flags;
}
按下添加警报活动中的保存按钮后服务关闭,如下所示:
//set time into calendar instance
Calendar calendar= Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,mAlarmDetails.timeHour);
calendar.set(Calendar.MINUTE,mAlarmDetails.timeMinute);
AlarmManager reminder = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmService.class);
PendingIntent pendingIntent = PendingIntent.getService(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
reminder.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),pendingIntent);
提前谢谢你
更新 我尝试将我的服务变成一个 Activity 并将此代码插入:
public class AlarmService extends Activity {
private MediaPlayer mMediaPlayer;
private PowerManager.WakeLock mWakeLock;
@Override
public void onCreate(Bundle savedInstateState) {
super.onCreate(savedInstateState);
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(/***PowerManager.FULL_WAKE_LOCK*/WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, "My Wake Log");
mWakeLock.acquire();
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
setContentView(R.layout.activity_alarm_screen);
}
我还将待处理意图中的 getService 从按钮更改为 getActivity。但是在我改变了这些之后,什么都没有出现。闹钟没有出现。
【问题讨论】:
标签: android service alarmmanager alarm android-wake-lock