Я пытаюсь установить триггер на 5 часов утра каждое утро, чтобы подготовить телефон к трансляции USER_PRESENT, но триггер сработает, как только служба будет запущена.Служба (PAService) отключается и включается включением основного действия с помощью stopService и startService .Этот код отлично работает в эмуляторе, но не на реальном телефоне Android.
public class PAService extends Service {
static boolean is_ready_to_speak = false;
PendingIntent pendingIntent;
public PAService() {
}
public class ScreenReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(is_ready_to_speak)
{
PASpeak paspeak = new PASpeak();
paspeak.sayit(getApplicationContext(),"You're using your phone for the first time this morning");
is_ready_to_speak = false;
}
}
}
public static class AlarmReceiver extends BroadcastReceiver {
public AlarmReceiver()
{
Log.d("AlarmReceiver func called","alarm receiver func called");
}
@Override
public void onReceive(Context context, Intent intent) {
Log.d("RECEIVED BROADCAST", "Sometype of ALARM Broadcast received");
PASpeak paspeak = new PASpeak();
paspeak.sayit(context.getApplicationContext(),"ALARM ALARM ALARM");
is_ready_to_speak = true;
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
IntentFilter filter = new IntentFilter(Intent.ACTION_USER_PRESENT);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
PASpeak paspeak = new PASpeak();
paspeak.sayit(getApplicationContext(),"process has started");
Intent alarmIntent = new Intent(PAService.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(PAService.this, 1, alarmIntent, 0);
// Set the alarm to start at 5:00 AM
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 5);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, // for repeating
// in every 24
// hours
pendingIntent);
return Service.START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}