I am trying to register an alarm with the android alarm manager and cannot seem to figure out why the the alarms aren't firing. I've looked at many examples online and my code seems to be doing the same thing that they suggest but still no success. I am running it on android 4.4 (KitKat)
Here is how I set the alarm:
public void registerAlarm(Context context, int hour, int minute) {
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(
Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReciever.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context,
0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10000,
alarmIntent);
}
and here is how my receiver for it:
package com.alarm.alarm;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class AlarmReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "recieved", Toast.LENGTH_LONG).show();
}
}
Here is how I declare my receiver in the manifest
<receiver android:name="com.alarm.alarm.AlarmReceiver">
</receiver>
and here is the SET ALARM permission
<uses-permission android:name="com.alarm.permission.SET_ALARM"/>
I have been stuck on this issue for hours. any clarification as to what i might be doing wrong would be greatly appreciated!
You have a typo in Broadcast class name, it should be AlarmReceiver instead of AlarmReciever per your Manifest
Related
I want my app to run a specific task at exactly 9 pm every day. I have read multiple examples of it being implemented in Android apps, and I have tried almost every one of them, but nothing seems to work. To test the code out, I try to show a toast message at a certain time, but no toast message comes up, even minutes later after the set time because I read on some sites that the Alarm time may not be exact. Please help me figure out what the problem with my code is.
MainActivity.java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 101, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 21);
calendar.set(Calendar.MINUTE, 00);
if (alarmManager != null) {
alarmManager.setExact(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
}
}
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(MainActivity.this, "Alarm...", Toast.LENGTH_SHORT).show();
}
}
AndroidManifest.xml:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
Any help is highly appreciated. I have been stuck with this issue for hours. Thank you.
I'm trying to set my alarm manager working, just simple schedule, firing toast every minute, but it's not working, what's wrong with the code?
Main Activity :
public void klik(View view) {
startalarm();
}
public void startalarm(){
AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent;
PendingIntent pendingIntent;
intent = new Intent(this, AlarmToastReciever.class);
pendingIntent = PendingIntent.getBroadcast(this,0,intent,0);
manager.setRepeating(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime()+3000,+60000,pendingIntent);
}
}
AlarmToastReciever class :
public class AlarmToastReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent){
Toast.makeText(context,"GOWNO", Toast.LENGTH_SHORT).show();
}
}
As stated in documentation
As of Android 4.4 (API Level 19), all repeating alarms are inexact. Note that while setInexactRepeating() is an improvement over setRepeating(), it can still overwhelm a server if every instance of an app hits the server around the same time. Therefore, for network requests, add some randomness to your alarms, as discussed above.
You can use "setInexactRepeating()" or set an exact one time alarm then set next alarm in On Receive method
Also make sure you added your receiver to the manifest file, between application tag, like
<receiver android:name=".AlarmToastReciever"
android:enabled="true">
<intent-filter>
</intent-filter>
</receiver>
Use this code to initialize alarm manager.
public void setupAlarm() {
final Calendar calNow = Calendar.getInstance();
final Calendar calSet = (Calendar) calNow.clone();
calSet.set(Calendar.HOUR_OF_DAY, calNow.get(Calendar.HOUR_OF_DAY));
calSet.set(Calendar.MINUTE, calNow.get(Calendar.MINUTE) + 1);
calSet.set(Calendar.SECOND, calNow.get(Calendar.SECOND));
final Intent intent = new Intent(this, UploadStarterReceiver.class);
final PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1,
intent, 0);
final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(),
60 * 1000, pendingIntent);
}
I am new to Android and I would appreciate any help on this.
I am trying to use Alarm Manager so that it would call my BroadcastReceiver class at a specific time in future set by DatePicker and TimePicker widgets.
However, i cannot get the broadcastreceiver class to perform its function.
below is the code that i put in the manifest file for the broadcastreceiver class to receive the broadcast message from OS:
<receiver android:name="androidapp.scheduler.MyBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.MYBROADCASTRECEIVER" >
</action>
</intent-filter>
</receiver>
And below is the code that i have put in my Next button's onclicklistener so that it would inform OS of the time of the call.
TimePicker myTp = (TimePicker)findViewById(R.id.tpTime);
cal.set(Calendar.YEAR,myDatePicker.getYear() );
cal.set(Calendar.MONTH, myDatePicker.getMonth());
cal.set(Calendar.DAY_OF_MONTH, myDatePicker.getDayOfMonth());
cal.set(Calendar.HOUR, myTp.getCurrentHour());
cal.set(Calendar.MINUTE, myTp.getCurrentMinute());
cal.set(Calendar.SECOND, 0);
Log.i("SCheduling", cal.getTime().toString());
Intent intent = new Intent(Scheduling.this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Scheduling.this, 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
Intent nextForm = new Intent(this,Sender.class);
nextForm.putExtra("contacts", contacts);
startActivity(nextForm);
Log.i("SCheduling", "everything set");
Finally, MybroadcastReceivers class code is shown below:
public class MyBroadcastReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Log.i("brreceiver", "BroadcastReciever");
Toast.makeText(context, "schedule set!.",
Toast.LENGTH_LONG).show();
}
}
What am I missing here?
In the following statement change Scheduling.this to this.getApplicationContext()
PendingIntent pendingIntent = PendingIntent.getBroadcast(Scheduling.this, 234324243, intent, 0);
it seems the code was correct, the problem was with the Emulator. I still don't know what was wrong with the emulator but i tested the code again and it worked and i did receive the log's output.
Got another problem. Trying to use a broadcastreceiver to catch off an Alarm (alarm manager).
The following code is used:
package com.suncco.shangxinbao.service;
import java.util.Calendar;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import com.suncco.shangxinbao.model.ScheduledSms;
public class SmsTimerService extends BroadcastReceiver {
public SmsTimerService(Context context, int timeoutInSeconds,
ScheduledSms sms) {
Intent myIntent = new Intent(context, SmsTimerService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
sms.getRequestCodeForListener(), myIntent, 0);
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 5); // should be timeoutInSeconds instead of 5
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
pendingIntent);
}
#Override
public void onReceive(Context context, Intent intent) {
Log.w("Karl", "yay");
}
In the manifest I put this as receiver:
<receiver
android:name="com.suncco.shangxinbao.service.SmsTimerService"
android:process=":remote" />
And I make an object of my SmsTimerService like this:
SmsTimerService smsTimerService = new SmsTimerService(this,5,sms);
And this is the well known error...
java.lang.RuntimeException: Unable to instantiate receiver
com.suncco.shangxinbao.service.SmsTimerService: java.lang.InstantiationException:
com.suncco.shangxinbao.service.SmsTimerService
You're getting the unable to instantiate receiver error because you don't have an empty constructor such as SmsTimerService(). The point is you need an empty constructor for this receiver to be created when receiving the intent. And it's ok if you don't implement any constructor. However, if you implement a constructor with parameters, you also need to implement an empty one!
So you can do something like this. Note that I didn't study what you actually want to do when creating intents.
public class SmsTimerService extends BroadcastReceiver {
private void doAction(Context context, int timeoutInSeconds, ScheduledSms sms) {
Intent myIntent = new Intent(context, SmsTimerService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
sms.getRequestCodeForListener(), myIntent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 5); // should be timeoutInSeconds instead of
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
#Override
public void onReceive(Context context, Intent intent) {
// you get to replace sms with something!
doAction(context, 5, sms); // original u do - > new SmsTimerService(this,5,sms);
}
}
In my app, I've got a BroadcastReceiver, which is called by an AlarmManager. That BroadcastReceiver calls CommonsWare's WakefulIntentservice.
I tested this on my phone, and it appeared that sometimes, my BroadcastReceiver isn't called at all. I'm really confused about what it could be. My BroadcastReceiver and WakefulIntentservice are registerd in the manifest.
This is my code:
In AlarmActivity:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 2);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), savedIntervalAutomaticMilisInt, pendingIntent);
Toast.makeText(this, "Saved", Toast.LENGTH_LONG).show();
finish();
AlarmReceiver:
package com.something.app;
import com.commonsware.cwac.wakeful.WakefulIntentService;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, AlarmService.class);
WakefulIntentService.sendWakefulWork(context, i);
}
}
And AlarmService:
package com.something.app;
import android.app.PendingIntent;
import android.content.Intent;
import com.commonsware.cwac.wakeful.WakefulIntentService;
public class AlarmService extends WakefulIntentService {
public AlarmService() {
super("AlarmService");
}
#Override
protected void doWakefulWork(Intent arg0) {
//A looooooooot of stuff
}
Does anyone know why the BroadcastReceiver sometimes isn't called?
EDIT: I heard about setting a BroadcastReceiver which receives onBootCompleted. Is that required?
So, that's the problem: If the device reboots, it sometimes clears the alarm, so you have to reset them in a BroadcastReceiver which receives onBootCompleted