How to alert alarm message when outside of the app in android? - android

I am trying this code to popup the alarm message. Its working when launching or opening the app, but it doesn't say any popup message while outside of the app.
I am so confused, i don't know what i am doing wrong.
String alarmtime = cur.getString(cur.getColumnIndex(DBDATA.LG_ALARMTIME));
//Reminder
String[] timesplit = alarmtime.split(":");
int hour = Integer.parseInt(timesplit[0]);
int minute = Integer.parseInt(timesplit[1]);
System.out.println(hour);
System.out.println(minute);
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, ShortTimeEntryReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar alarm = new GregorianCalendar();
alarm.setTimeInMillis(System.currentTimeMillis());
alarm.set(Calendar.HOUR_OF_DAY, hour);
alarm.set(Calendar.MINUTE, minute);
alarm.set(Calendar.SECOND, 0);
System.out.println(System.currentTimeMillis());
System.out.println(alarm.getTimeInMillis());
if (System.currentTimeMillis() > alarm.getTimeInMillis()){
alarm.setTimeInMillis(alarm.getTimeInMillis()+ 24*60*60*1000);// Okay, then tomorrow ...
alarmMgr.set(AlarmManager.RTC_WAKEUP, alarm.getTimeInMillis(),pendingIntent);
}
else
{
alarmMgr.set(AlarmManager.RTC_WAKEUP, alarm.getTimeInMillis(),pendingIntent);
}
I need to pop up the alarm message outside of the app(i.e) exactly like the alarm does.
Thanks for your help guys,

You probably need a BroadcastReceiver.
As you can read in this question : BroadcastReceiver not receiving an alarm's broadcast
You have to build the intent like this :
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(this, "CHECK_ALARM_CODE", alarmIntent, 0);
And receive the alarm like this :
public class AlarmReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
Log.d("OK", "AlarmReceiver.onReceive");
}
}
Don't forget to register your broadcast in your manifest file.

Related

Alarm Manager is not working in Android

I need to setup an Alarm in some interval of times. To achieve it I wrote:
TestFragment class
private void setupAlarmManager(){
AlarmManager manager = manager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(getContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(), i, alarmIntent, PendingIntent.FLAG_ONE_SHOT);
manager.set(AlarmManager.RTC_WAKEUP,1499510100000L, pendingIntent);
manager.set(AlarmManager.RTC_WAKEUP,1499510220000L, pendingIntent);
}
AlarmReceiver class
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
}
}
I put the debug point at Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show(); but nothing happened.
4PM = 1499510100000L
4:03PM = 1499510220000L
What Am I doing wrong here? Further I want to add a Local notification in onReceive method.
You should use a Calendar object to set up the alarm time.
For an alarm at 4PM you could do something like:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 16);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND,0);
Then you set up your AlarmManager as you already did but for the last line use:
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Maybe you want to use a repeating Alarm or even a Handler
If so you should visit: https://developer.android.com/training/scheduling/alarms.html

Android: My broadcast receiver for alarm manager isn't working

I want my alarm manager to be set for a certain time to change the phone background, but the broadcast receiver isn't doing anything. Can you help tell me why?
Scheduling alarm:
public void scheduleAlarm(Context context){
Intent intent = new Intent(context, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 18);
calendar.set(Calendar.MINUTE, 34);
am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
am.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent );
Toast.makeText(context, "Alarm set", Toast.LENGTH_LONG).show();
}
Broadcast Receiver:
#Override
public void onReceive(Context context, Intent intent){
PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
GrilledCheeseLookup.getGrilledCheeseJSON(grilledCheeseUrls, context);
Toast.makeText(context, "Alarm worked", Toast.LENGTH_LONG).show();
}
Enabling in manifest:
<receiver android:name=".AlarmReceiver" android:enabled="true" />
Try creating your Intent while also specifying the packageContext, using the Intent/4 constructor (you can set the uri to null).

Android Notification starting up on launch instead of at the set time

I am currently trying to create a repeating alarm that goes off at the same time everyday following a tutorial however I cannot seem to get it working. It goes off but as soon as the application launches instead of the set time I am not sure what the reason is. I also made sure the service was declared in manifest. Any idea where I am going wrong or if this the right way to go about something like this?
Main acitivty
public class MainActivity extends AppCompatActivity {
private PendingIntent pendingIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
int i = preferences.getInt("numberofLaunces", 1);
if (i < 2) {
alarmMethod();
i++;
editor.putInt("numberoflaunches", i);
editor.commit();
}
if (savedInstanceState == null) {
return;
}
}
private void alarmMethod() {
Intent myIntent = new Intent(this, NotifyService.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 32);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
calendar.set(Calendar.DAY_OF_MONTH, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 60 * 24, pendingIntent);
Toast.makeText(MainActivity.this, "start Alarm", Toast.LENGTH_LONG).show();
}
}
Notifyservice
public class NotifyService extends Service {
#Override
public IBinder onBind(Intent Intent) {
return null;
}
#Override
public void onCreate(){
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(), MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1,0);
Notification mNotify = new Notification.Builder(this)
.setContentTitle("placeholder tital")
.setContentText("Placeholder text")
.setSmallIcon(R.drawable.ic_cat)
.setContentIntent(pIntent)
.setSound(sound)
.addAction(0,"hello", pIntent)
.build();
mNM.notify(1, mNotify);
}
}
From developer [site](http://developer.android.com/reference/android/app/AlarmManager.html#set(int, long, android.app.PendingIntent))
The alarm is an Intent broadcast that goes to a broadcast receiver that you registered with registerReceiver(BroadcastReceiver, IntentFilter) or through the tag in an AndroidManifest.xml file.
You should define broadcast pending intent instead of service.
Change this
Intent myIntent = new Intent(this, NotifyService.class);
pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
To this
Intent myIntent = new Intent(this, MyAlarmBroadcastReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, 0);
Then implement MyAlarmBroadcastReceiver extends BroadcastReceiver class and fire notification inside onReceive().
The problem is quite simple: If the time a alarm should fire (the second parameter in setRepeating()) is in the past, it will fire immediately. The time you specified is 0:32 am in the night of the 0th (?) day of the month.
This is always in the past (maybe except for 32 minutes every month, though I don't know what the 0th day of the month is).
To fire the alarm every 24 hours at the time of 0:32 am use something like this:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.MINUTE, 32);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
calendar.add(Calendar.DAY_OF_MONTH, new Date().after(calendar.getTime()) ? 1 : 0); //if the current time is after 0:32 am, one day is added
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 60 * 24, pendingIntent);

Why an Alarm Broadcast receiver not receiving broadcast?

I am trying to start a service at specific intervals with help of a BroadcastReceiver. I have defined the receiver as an inner class in service itself as follows:
public class AlarmReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("RTESPLDebug", "Recieved Broadcast!"); //Never appears in LogCat
//Intent i = new Intent(context, LocationUpdateService.class);
context.startService(intent);
}
};
I register the alarm receiver in service's onCreate():
context = super.getApplicationContext();
AlarmReceiver ar = new AlarmReceiver();
IntentFilter intfilter = new IntentFilter(Intent.ACTION_DEFAULT);
intfilter.addCategory(Intent.CATEGORY_DEFAULT);
registerReceiver(ar, intfilter);
I first start the service from my first activity and then register a single alarm there:
Intent i = new Intent(context, LocationUpdateService.class);
context.startService(i);
// Schedule first alarm
int nextUpdateInterval = 30*1000; // Let first alarm be after 30 sec
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(context, LocationUpdateService.class);
intent.setAction(Intent.ACTION_DEFAULT);
intent.addCategory(Intent.CATEGORY_DEFAULT);
PendingIntent pintent = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis()+nextUpdateInterval, pintent);
In the service I register for a single location update, and when I receive update (I do receive), I process it (I have debugged this, processing indeed completes without errors), and then I register next alarm in similar way as above:
int nextUpdateInterval = getNextUpdateInterval(); // Returns 30000
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(context, LocationUpdateService.class);
intent.setAction(Intent.ACTION_DEFAULT);
intent.addCategory(Intent.CATEGORY_DEFAULT);
PendingIntent pintent = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis()+nextUpdateInterval, pintent);
Lots of code and a long questions.

android alarmmanager + task schedule

I need help. Im new to android coding.
I have made task list, which I want to do specific things at time written in task.
Here is my task item
private long id;
private int mon;
private int tues;
private int wednes;
private int thurs;
private int fri;
private int satur;
private int sun;
private int profile;
Where I have days (monday,tuesday etc) which holds amount of minutes (for 10:00 its 600).
Following some tutorials I have alarm reciever
public class AlarmReciever extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
try {
Bundle bundle = intent.getExtras();
String message = bundle.getString("alarm_message");
Intent newIntent = new Intent(context, AlarmActivity.class);
newIntent.putExtra("profile", message);
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(newIntent);
} catch (Exception e) {
Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
Its still unedited...
And then there is code which calls to make new tasks in alarm manager
// get a Calendar object with current time
Calendar cal = Calendar.getInstance();
// add 5 minutes to the calendar object
cal.add(Calendar.MINUTE, 5);
Intent intent = new Intent(ctx, AlarmReceiver.class);
intent.putExtra("alarm_message", "O'Doyle Rules!");
// In reality, you would want to have a static variable for the request code instead of 192837
PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
I dont understand how to specify in calendar, that I need new task repeating (for example) every monday at 10:00, and that when this happens, it calls new function, giving it "profile" variable to work with.
private void setProfile(Integer profile)
{
// Doing things with profile
}
take a look at the following code:
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent2 = new Intent(context, SampleAlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent2, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
// Set the alarm's trigger time to item hour
calendar.set(Calendar.HOUR_OF_DAY, NuevoItemActivity.hora.getCurrentHour());
calendar.set(Calendar.MINUTE, NuevoItemActivity.hora.getCurrentMinute());
// Set the alarm to fire , according to the device's clock, and to repeat once a day.
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, (PendingIntent) alarmIntent);
As you can see in last line, you are able to indicate AlarmManager.INTERVAL_DAY to repeat the PendingIntent.

Categories

Resources