Search code examples
androidbroadcastreceiverandroid-manifestalarmmanager

My AlarmManager not getting called


I try every code which I got from Stackoverflow or from anywhere, but not getting success in AlarmManager, my BroadcastReceiver not even getting called. Here is my code:

    //My AlarmManager is in MyDB.Class extends SQLiteOpenHelper{

                System.out.println("Inside AlarmManager");
                // Create alarm manager
                AlarmManager alarmMgr0 = (AlarmManager) mContext
                        .getSystemService(Context.ALARM_SERVICE);

                System.out.println("Inside alarmMgr0     " + alarmMgr0);

                // Create pending intent & register it to your alarm notifier
                // class
                Intent intent0 = new Intent(mContext, AlarmReciever.class);
                intent0.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent0.putExtra("req_code", v); // pI);
                PendingIntent pendingIntent0 = PendingIntent.getBroadcast(
                        mContext, v, intent0, 0);
                System.out.println("Inside pendingIntent0     "
                        + pendingIntent0);

                // set timer you want alarm to work
                Calendar timeOff9 = Calendar.getInstance();
                timeOff9.add(Calendar.DAY_OF_YEAR, scheduleDao1.getMeetingDay());
                System.out.println("Inside if getDay    "
                        + scheduleDao1.getDay());
                timeOff9.set(Calendar.MONTH, scheduleDao1.getMeetingMonth());
                System.out.println("Inside if getMonth    "
                        + scheduleDao1.getMonth());
                timeOff9.set(Calendar.YEAR, scheduleDao1.getMeetingYear());
                System.out.println("Inside if getYear    "
                        + scheduleDao1.getYear());

                if (scheduleDao1.getFromTimeHr() != 00) {
                    System.out.println("Inside if getFromTimeHr    "
                            + (scheduleDao1.getFromTimeHr() - 1));

                    timeOff9.set(Calendar.HOUR_OF_DAY,
                            scheduleDao1.getFromTimeHr() - 1);
                }
                timeOff9.set(Calendar.MINUTE,
                        scheduleDao1.getFromTimeMin());
                System.out.println("Inside getMeetingFromTimeMin     "
                        + scheduleDao1.getFromTimeMin());
                timeOff9.set(Calendar.SECOND, 0);

                // set that timer as a RTC Wakeup to alarm manager object
                alarmMgr0.set(AlarmManager.RTC_WAKEUP,
                        timeOff9.getTimeInMillis(), pendingIntent0);

                contents.put(ALARM_REQUEST_CODE, v);

                //}

My Receiver class is here (Which is not get even called)

public class AlarmReciever extends BroadcastReceiver {
    Context mContext;
    int request_code = 0;

@Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        mContext = context;
        System.out.println("Inside onReceive");
        mContext = context;
        request_code = intent.getIntExtra("req_code", 0);
        System.out.println("Inside request_code       " + request_code);

//      Bundle extras = intent.getExtras();
//      request_code = extras.getInt("req_code");
//      System.out.println("Inside request_code       " + request_code);

      }
}

And my Manifest code is like that....

.
.
.
<receiver android:name=".AlarmReciever" 
            android:enabled="true"/>
    </application>

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

I didn't get what is going wrong with these code please someone help me here.


Solution

  • Finally i got What i want, with the help of @kgandroid & @rainash Here i post my Solve code now. which is working fine for me & Showing Notifications. And i also able to set here Multiple AlarmManager with my required time.

    // Create alarm manager
       AlarmManager alarmMgr0 = (AlarmManager) mContext
    		.getSystemService(Context.ALARM_SERVICE);
    				
    		// Create pending intent & register it to your alarm notifier
    				// class
    		Intent intent0 = new Intent("alarm");
    		intent0.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    		intent0.putExtra("req_code", v); // pI);
    		PendingIntent pendingIntent0 = PendingIntent
    				.getBroadcast(mContext, v, intent0,
    						PendingIntent.FLAG_UPDATE_CURRENT);
    
    		// set timer you want alarm to work
    	Calendar timeOff9 = Calendar.getInstance();
    	timeOff9.setTimeInMillis(System.currentTimeMillis());
    	timeOff9.set(scheduleDao1.getYear(),
    		(scheduleDao1.getMonth()) - 1,  //Months start from '0'
    		scheduleDao1.getDay(),
    	(scheduleDao1.getFromTimeHr()) - 1,//I want those notification before 1hr
    			scheduleDao1.getFromTimeMin(), 0);
    
    	// long dateWeGet = Long.valueOf(dateString);
       // set that timer as a RTC Wakeup to alarm manager object
    		alarmMgr0.set(AlarmManager.RTC_WAKEUP,
    				timeOff9.getTimeInMillis(), pendingIntent0);
    
    contents.put(ALARM_REQUEST_CODE, v);