Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Alarm Manager with broadcast receiver registered in code rather than manifest

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?

like image 566
Shane Avatar asked Aug 10 '10 09:08

Shane


People also ask

How do you check broadcast receiver is registered or not in Android?

You can put a flag into your class or activity. Put a boolean variable into your class and look at this flag to know if you have the Receiver registered. Create a class that extends the Receiver and there you can use: Singleton pattern for only have one instance of this class in your project.

How do you declare a broadcast receiver in manifest?

There are two ways to make a broadcast receiver known to the system: One is declare it in the manifest file with this element. The other is to create the receiver dynamically in code and register it with the Context. registerReceiver() method.

Where do I register and unregister broadcast receiver?

Receiver can be registered via the Android manifest file. You can also register and unregister a receiver at runtime via the Context. registerReceiver() and Context. unregisterReceiver() methods.

Does broadcast receiver work in background?

As an Android developer, you'll often run into the scenario where you need to perform tasks and display notifications for your app in the background. To retain battery power on our users device we are going to run background tasks using a broadcast receiver.


2 Answers

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

like image 158
user123321 Avatar answered Oct 23 '22 13:10

user123321


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!

like image 36
Dhrupal Avatar answered Oct 23 '22 14:10

Dhrupal