Search code examples
androidemulationalarmmanager

Android AlarmManager not working on Emulator


Another complain about AlarmManager (hoping for a quick solution). I am using the Android Emulator for developing. I found an allegedly working example so I tried to use it. I did the following:

  1. Added a receiver string to the manifest file.

     <receiver android:name=".SchHandler" android:process=":remote" />
    
  2. Created the main activity and used its onCreate.

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Bundle bundle = new Bundle();
        SchHandler handler = new SchHandler(this, bundle, 1);
    }
    
  3. Created a BroadcastReceiver to create and listen to alarms.

    public class SchHandler extends BroadcastReceiver {
        private final String REMINDER_BUNDLE = "ReminderBundle";
    
        public SchHandler (Context context, Bundle extras, int timeoutInSeconds) {    
            Toast.makeText(context, "Scheduling...", Toast.LENGTH_LONG).show();
            Log.d("Debug", "Sch");
    
            AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            Intent intent = new Intent(context, SchHandler.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    
            Toast.makeText(context, "Time:" + System.currentTimeMillis(), Toast.LENGTH_LONG).show();
            alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
            System.currentTimeMillis() + 2000, 5000, pendingIntent);    
        }
    
        @Override
        public void onReceive(Context context, Intent arg1) {
           // TODO Auto-generated method stub
           Log.e(REMINDER_BUNDLE, "Receive");
           Toast.makeText(context, "Testing", Toast.LENGTH_LONG).show();
        }
    }
    

I tried it with set, setRepeating, nothing worked. What else should I try?


Solution

  • I modified the name of my receiver to its fully qualified version, and it turned out that I used its package name instead of the class name. After setting it properly, I got it working.