带有时间和日期选择器的Android Alarm Manager

时间:2015-08-28 07:53:32

标签: android datepicker broadcastreceiver alarmmanager timepicker

我正在开发一个应用程序,我需要用户来安排活动。我使用TimePicker和DatePicker对话框来设置事件的时间,并使用带有BroadcastReceiver的AlarmManager在达到时间时执行操作。在测试时,我得出结论,我的时间和日期选择器对话框工作正常,我可以选择日期和时间。但是,当达到时间时,AlarmManager不会触发BroadcastReceiver。

这是我的MainActivity.class,用于安排事件:

3432

AlarmReceiver.class:

public class MainActivity extends Activity {

Button setTime;
Button scheduleEvent;
static final int DATEPICKER_DIALOG_ID = 0;
static final int TIMEPICKER_DIALOG_ID = 1;
int dpYear, dpMonth, dpDay, tpHour, tpMinute;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Calendar calendar = Calendar.getInstance();
    dpYear = calendar.get(Calendar.YEAR);
    dpMonth = calendar.get(Calendar.MONTH);
    dpDay = calendar.get(Calendar.DAY_OF_MONTH);
    tpHour = calendar.get(Calendar.HOUR_OF_DAY);
    tpMinute = calendar.get(Calendar.MINUTE);

    setTime = (Button) findViewById(R.id.button1);
    scheduleEvent = (Button) findViewById(R.id.button2);

    setTime.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showDialog(DATEPICKER_DIALOG_ID);
        }
    });

    scheduleEvent.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent intent = new Intent("com.ohs.example.myEvent");
            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager alarmMgr = (AlarmManager) (this.getSystemService(Context.ALARM_SERVICE));

            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR, dpYear);
            cal.set(Calendar.MONTH, dpMonth);
            cal.set(Calendar.DAY_OF_MONTH, dpDay);
            cal.set(Calendar.HOUR_OF_DAY, tpHour);
            cal.set(Calendar.MINUTE, tpMinute);
            cal.set(Calendar.SECOND, 0);
            long mills = cal.getTimeInMillis();

            alarmMgr.set(AlarmManager.RTC_WAKEUP, mills, pendingIntent);
            Toast.makeText(this, "Event scheduled at " + tpHour + ":" + tpMinute + " " + dpDay + "/" + dpMonth + "/" + dpYear, Toast.LENGTH_LONG).show();

        }
    });

}

@Override
protected Dialog onCreateDialog(int id) {
    if (id == DATEPICKER_DIALOG_ID) {
        return new DatePickerDialog(this, datePickerListener, dpYear, dpMonth, dpDay);
    } else if (id == TIMEPICKER_DIALOG_ID) {
        return new TimePickerDialog(this, timePickerListener, tpHour, tpMinute, false);
    } else {
        return null;
    }
}

private DatePickerDialog.OnDateSetListener datePickerListener =
        new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                dpYear = i;
                dpMonth = i1 + 1;
                dpDay = i2;
                showDialog(TIMEPICKER_DIALOG_ID);
            }
        };

protected TimePickerDialog.OnTimeSetListener timePickerListener =
        new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int i, int i1) {
                tpHour = i;
                tpMinute = i1;
            }
        };
}

的AndroidManifest.xml:

public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    Toast.makeText(context, "Broadcast Received", Toast.LENGTH_LONG).show();
    eventMethod();

}

protected void eventMethod() {

//Do something...

}
}

我之前曾在其他应用程序上工作过,我每隔6小时就要设置一次重复闹钟。我使用了与上面完全相同的代码(使用AlarmManager和BraodcastReceiver),而不是:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ohs.example" >

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".AlarmReceiver" android:enabled="true">
        <intent-filter>
            <action android:name="com.ohs.example.myEvent" />
        </intent-filter>
    </receiver>
</application>

</manifest>

我所拥有的只是:

    cal.set(Calendar.YEAR, dpYear);
    cal.set(Calendar.MONTH, dpMonth);
    cal.set(Calendar.DAY_OF_MONTH, dpDay);
    cal.set(Calendar.HOUR_OF_DAY, tpHour);
    cal.set(Calendar.MINUTE, tpMinute);
    cal.set(Calendar.SECOND, 0);

它完美无缺。所以&#34;年&#34;,&#34; MONTH&#34;和&#34; DAY_OF_MONTH&#34;在这里造成问题的那些。如果是,那么解决方案是什么?

我已经看了很多解决问题的方法,我在stackoverflow上发现了类似的问题,但没有人帮助过我。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我有一次这个问题。所有测试手机都会出现这种现象吗?我的问题出现是因为我的一个测试手机修改了手机硬件以解决省电问题。所以alarmReceiver在evey五分钟