I using a Alarmmanager in the launch activity to execute something in BroadcastReceiver repeatedly, but BroadcastReceiver not being triggered. my code is as follow:
MainActivity.class:
AlarmManager manager = (AlarmManager) getBaseContext().getSystemService(Context.ALARM_SERVICE);
Intent gpsintent =new Intent(getBaseContext(),gps_sendback_alarm_receiver.class);
PendingIntent.getService(getBaseContext(), 0, gpsintent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar now = Calendar.getInstance();
manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, now.getTimeInMillis(), int_gps_sendback_period, pendingIntent);
gps_sendback_alarm_receiver.class:
public void onReceive(Context context, Intent intent) {
//send back the GPS location
}
manifest:
<application>
<receiver android:name="com.example.xmobiler.gps_sendback_alarm_receiver" android:enabled="true"/>
</application>
anybody can help?
you are creating the variable gpsintent but the alarmmanager gets pendingIntent that is not assigned in your example code
Related
I am developing one android application, In which i need to call onReceive method of Alarmmanager from onReceive of another BroadcastReceiver i.e. Internet connectivity. Is it possible ? Or should i duplicate all my stuff in another BroadcastReceiver?
You can make a new intent from onReceive to trigger another broadcast receiver
#Override
public void onReceive(Context context, Intent intent) {
Intent newIntent = new Intent("com.domain.yourboardcastreceiver");
context.sendBroadcast(newIntent);
}
In OnReceive(..) method of Internet Connectivity broadcast receiver, you can set alarm and thats how alarm manager will get triggered, eg :
#Override
public void onReceive(Context context, Intent intent) {
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());
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
Don't forget to register alarmmanager's receiver in your manifest file.
Hope it helps !
I have searched in everywhere but I can understand because my Service at a specific time of the day don't fire. Nothing happens.
My code for set up the Alarm:
AlarmManager alarmMgr = (AlarmManager) act.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(act, AlarmReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(act, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
// set for 5 seconds later
alarmMgr.set(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis()+5000, alarmIntent);
My code for the Service:
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("Alarm worked well");
Utils.criarNotificacao(context);
}
}
In the Manifest:
<receiver android:name="br.com.blacktoad.q48h2.utils.AlarmReceiver" android:process=":remote"></receiver>
I can't find any answer.
I find the error!
I need put the:
<receiver android:name="br.com.blacktoad.q48h2.utils.AlarmReceiver" android:process=":remote"></receiver>
in the manifest, but contained in the "application" tag
I have a service that runs in background. I am using alarm manager to start this service. It works fine but when i change the system clock in my device or in the simulator the alarm manager stops.
public void startAzanService() {
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(this, CheckAzan.class);
PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
//repeat the action every 5 secionde
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 5000, pintent);
}
You can overcome this problem by Listening to the time changing Broadcast. Do the following:-
1)Create BroadcastReceiver in you Manifest file:-
<receiver android:name=".TimeChangeReceiver">
<intent-filter >
<action android:name="android.intent.action.TIME_SET"/>
</intent-filter>
</receiver>
2)Create the receiver class:-
public class TimeChangeReceiver extends BroadcastReceiver{
#Override
public void onReceive(final Context arg0, Intent arg1) {
//Call your Alarm setting code
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(this, CheckAzan.class);
PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
//repeat the action every 5 secionde
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 5000, pintent);
}
}
*Assuming that your don`t have any problem with your code :)
This link is also a good Tutorial of the BroadcastReceiver
I have already read some tutorials and read the documentation, but I can't make this work... This is what I have been testing:
This is the way I register the intent to be called with the alarm manager:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, 1)
PendingIntent sender = PendingIntent.getBroadcast(this, 192837, new Intent(this, AlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
My AlarmReceiver:
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent intent2 = new Intent(context, NewCommit.class);
context.startActivity(intent2);
}
}
And of course I added the receiver in the androidManifest.xml:
<receiver android:process=":remote" android:name="AlarmReceiver"></receiver>
And is INSIDE the application tag.
Any idea? It's driving me crazy, I can't find what's wrong!
Thanks
I just added to the intent from pendingIntent:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
I hope in helps someone!
You need to put permission in AndroidManifest.xml
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
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?
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.)
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!