-2

In my app I want a particular event to fire up when that event date and time comes.I have done some research and wrote this code. But it doest seems working. Please do help me out

MainActivity

@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);
    }

Alarmreceived

public class AlarmReceived extends BroadcastReceiver {


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

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

    }
}

Manifest

 <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>

I don't know what the problem is? Please do help me out.

Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45
Anuj
  • 367
  • 2
  • 6
  • 18
  • Use this Link Will help you- I have used Working fine [For Triggering Alert in Particular time in android][1] [1]: http://stackoverflow.com/a/30452831/4447803 – Android Dev May 30 '15 at 08:20

3 Answers3

0

Change your hour of day to 13 for 1 pm

cal.set(2015, 05, 30, 1, 22); //here change change 1 to 13
Pankaj
  • 7,908
  • 6
  • 42
  • 65
0

Use this Link Will help you- I have used Working fine

For Triggering Alert in Particular time in android

[Link]

https://stackoverflow.com/a/30452831/4447803

link 2

Community
  • 1
  • 1
Android Dev
  • 421
  • 8
  • 26
0

I solved it. My code was working fine but it was problem of the OS. In java or android the Calender month starts from 0 so here January wont be 1 it will be 0 and so on. So to solve this just do

Month-1

to get the current month

Anuj
  • 367
  • 2
  • 6
  • 18