AlarmManager 未在所需的时间和日期启动

AlarmManager not firing up on desired time and date

在我的应用程序中,我希望特定事件在事件日期和时间 comes.I 完成一些研究并编写此代码时触发。但它似乎确实有效。请帮帮我

主要活动

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

        initialize();
    }

    private void initialize() {
//        Calendar cal = Calendar.getInstance();
//        cal.set(Calendar.MONTH, 5);
//        cal.set(Calendar.YEAR, 2015);
//        cal.set(Calendar.DAY_OF_MONTH, 30);
//
//        cal.set(Calendar.HOUR_OF_DAY, 1);
//        cal.set(Calendar.MINUTE, 12);
//        cal.set(Calendar.SECOND, 0);
//        cal.set(Calendar.AM_PM,Calendar.PM);

        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(System.currentTimeMillis());
        cal.clear();
        cal.set(2015, 05, 30, 1, 22);

        Intent intent = new Intent(this, AlarmReceived.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
    }

收到警报

public class AlarmReceived extends BroadcastReceiver {


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

        Toast.makeText(context,"Yess",Toast.LENGTH_LONG).show();

    }
}

清单

 <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>

    <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=".AlarmReceived"></receiver>
    </application>

不知道是什么问题?请帮帮我。

将您的时间更改为下午 13 点

cal.set(2015, 05, 30, 1, 22); //here change change 1 to 13

用这个Link会帮助你-我用过很好用

在android

的特定时间触发警报

[Link]

link 2

我解决了。 我的代码工作正常,但这是 OS 的问题。 在 java 或 android 中,日历月份从 0 开始,所以这里的一月不会是 1,而是 0,依此类推。 所以要解决这个问题就做

Month-1

获取当前月份