13

I want to use an alarm to run some code at a certain time. I have successfully implemented an alarm with the broadcast receiver registered in the manifest but the way i understand it, this method uses a separate class for the broadcast receiver.

I can use this method to start another activity but I cant use it to run a method in my main activity?

(how can I notify a running activity from a broadcast receiver?)

So I have been trying to register my broadcast receiver in my main activity as explained in the answer above.

private BroadcastReceiver receiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "hello", Toast.LENGTH_SHORT).show();
        uploadDB();         
    }
};    

public void onResume() {
    super.onResume();

    IntentFilter filter = new IntentFilter();
    filter.addAction(null);

    this.registerReceiver(this.receiver, filter);
}

public void onPause() {
    super.onPause();

    this.unregisterReceiver(this.receiver);
}

However I have been unable to get this to work with alarm manager, I am unsure as to how i should link the alarm intent to the broadcast receiver. Could anyone point me to an example of registering an alarm manager broadcast receiver dynamically in the activity? Or explain how i would do this?

Community
  • 1
  • 1
Shane
  • 249
  • 1
  • 3
  • 10

2 Answers2

20

How about this?

Intent startIntent = new Intent("WhatEverYouWant");
PendingIntent startPIntent = PendingIntent.getBroadcast(context, 0, startIntent, 0);
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, triggerTime, startPIntent);

And then in your Manifest.xml file:

<receiver android:name="com.package.YourOnReceiver">
   <intent-filter>
       <action android:name="WhatEverYouWant" />
   </intent-filter>
</receiver>

So as far as I know you still have to declare the receiver in the Manifest. I'm not sure if you can set it to a private instance inside of your activity. You could declare an onReceive inside of your activity and call that (if the BroadcastReceiver has an interface. I don't know if it does.)

Ben Pearson
  • 7,532
  • 4
  • 30
  • 50
user123321
  • 12,593
  • 11
  • 52
  • 63
  • 3
    Awesome, I managed to get it working, I didn't need to add code to the manifest though. Just registered the broadcast receiver progamatically with an intentfilter of the same name. – serenskye Aug 17 '12 at 17:20
  • @serenskye My code is not working without registering it in manifest. How did you get away with this? – Shubham Agrawal Jul 20 '20 at 10:18
1

Start a alarm intent from where you want to start alarm. write below code from where you want to start to listen the alarm

Intent myIntent = new Intent(getBaseContext(), **AlarmReceiver**.class);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, myIntent, 0);
                AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(System.currentTimeMillis());
                calendar.add(Calendar.MINUTE, shpref.getInt("timeoutint", 30));
                alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

And in broadcast receiver write the code you want to receive. And in menifest write below

<receiver android:name=".AlarmReceiver" android:process=":remote"/>

You can also put repetitive alarm also. Hope it help!

Dhrupal
  • 1,863
  • 1
  • 23
  • 38