几个小时后setAlarmClock就失败了

时间:2017-06-01 07:55:45

标签: android alarmmanager

我正在制作闹钟应用程序。我正在使用setAlarmClock来设置它关闭的时间,而WakefulBroadcastReceiver则接受设置的警报。

当我使用测试它在1分钟或30分钟后响铃时,即使我杀了应用程序,闹铃也会完美响起。但是每当我在5小时后将其设置为响铃时,WakefulBroadcastReceiver就不再收到pendingIntent

这是我在全球课程中的代码:

public static void setAlarm (Context context, long timelong){
        SharedPreferences sharedPreferences = context.getSharedPreferences(KEY_PREFS, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putLong(KEY_SET_TIME, timelong);
        editor.commit();

        myToaster(context, "Alarm will ring after " + timeCompare(timelong, System.currentTimeMillis()));

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
        Intent intent = new Intent(context, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
        alarmManager.setAlarmClock(new AlarmManager.AlarmClockInfo(timelong, pendingIntent), pendingIntent);
        }

WakefulBroadcastReceiver

public class AlarmReceiver extends WakefulBroadcastReceiver{

    final private String TAG = "AlarmReceiver";

    @Override
    public void onReceive(Context context, Intent intent){
        Log.e(TAG, "received");
        Intent toRinger = new Intent(context, RingerActivity.class);
        toRinger.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Log.e(TAG, "Received and firing intent");
        context.startActivity(toRinger);
    }
}

然后,我在RingerActivity.class onCreate上设置了一个mediaPlayer环。

此外,这是我如何使用TimepickerFragment

调用setAlarm方法
public TimepickerFragment(){
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){
        //Use the current time as the default values for the time picker
        textView = (TextView) getActivity().findViewById(R.id.textView);
        Date d = new Date();
        int hour = d.getHours();
        int minute = d.getMinutes();

        //Create and return a new instance of TimePickerDialog
        return new TimePickerDialog(getActivity(),this, hour, minute,
                DateFormat.is24HourFormat(getActivity()));
    }

    //onTimeSet() callback method
    public void onTimeSet(TimePicker view, int hour, int minute){
        Long timelong = timeToLong(hour, minute);
        Globe.setAlarm(getContext(), timelong);

        String timeString = Globe.dateToTimeString(new Date(timelong));
        String alarmInfo = getText(R.string.alarm_info) + " " + timeString;
        textView.setText(alarmInfo);
    }

public static long timeToLong(int h, int m) {
        Date d = new Date();
        d.setHours(h);
        d.setMinutes(m);
        if (d.getTime() <= System.currentTimeMillis())
            return (d.getTime() + (1000 * 60 * 60 * 24));
        else
            return d.getTime();
    }

那么,任何人都可以告诉我为什么如果我将闹钟设置为超过1小时我的闹钟不起作用,我该如何解决?

1 个答案:

答案 0 :(得分:0)

在SO上发现了另一个问题,并编辑了我的代码。它完美地运作。但是,我试图再次找到它,但无济于事。所以,我把我的工作代码放在这里。

我编辑了我的WakefulBroadCastReceiver以启动WakefulService。然后,我将我的意图转移到了WingerfulService的Ringer Activity。

public class AlarmReceiver extends WakefulBroadcastReceiver{

    final private String TAG = "AlarmReceiver";

    @Override
    public void onReceive(Context context, Intent intent){
        Log.e(TAG, "received");
            Intent i = new Intent(context, AlarmReceiverService.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startWakefulService(context, i);
        }
    }
}

wakefulService

public class AlarmReceiverService extends IntentService {

    public AlarmReceiverService() {
        super("AlarmReceiverService");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        Log.e("AlarmReceiverService", "Service received");
        Intent i = new Intent(this, RingerActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(i);
        AlarmReceiver.completeWakefulIntent(intent);
    }
}