Android Clock App Set Alarm - android

I need to know if this is possible, and if it is, how I should proceed.
I'd like to make an app that will create a repeating alarm within the stock Android Clock App. It should go off at 8am each morning and should only take one button to activate within my app.
Thanks in advance

Try this:
public void createAlarm(String message, int hour, int minutes) {
Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM)
.putExtra(AlarmClock.EXTRA_MESSAGE, message)
.putExtra(AlarmClock.EXTRA_HOUR, hour)
.putExtra(AlarmClock.EXTRA_MINUTES, minutes);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
Source: https://developer.android.com/guide/components/intents-common.html

I found this way for Clock
public void createAlarm(String message, int hour, int minutes) {
Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM)
.putExtra(AlarmClock.EXTRA_MESSAGE, message)// show on it
.putExtra(AlarmClock.EXTRA_HOUR, hour) //24 hours
.putExtra(AlarmClock.EXTRA_MINUTES, minutes); //not more than 60 :)
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}

Related

How do I make an action at a specific time when the phone is locked?

I have this method. The problem is that it does not work when I lock the phone. It only shows me the notification if the phone has the screen on, if I block it and I activate it only 5 minutes later (it is 9:00 p.m. and I put it on at 9:05 p.m.), nothing happens. I read this questions, but I don't know how to start it at a specific time. Thanks in advance and please don't check this question as duplicate of this question
public void startBroucast(int a,int b) {
int minutes=a;
int hours=b;
Calendar c = Calendar.getInstance();
c.add(Calendar.HOUR,hours);
c.add(Calendar.MINUTE,minutes);
Timer time = new Timer();
time.schedule(new TimerTask() {
#Override
public void run() {
showNotification();
}
},c.getTime());
}
You should use AlarmManager to schedule tasks in the future. Here's how you can do that:
public void startBroucast(int minutes,int hours) {
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY,hours);
c.set(Calendar.MINUTE,minutes);
long timeInMillis = c.getTimeInMillis();
Intent notificationIntent = new Intent(getContext(),NotificationReceiver.class);
mNotificationId = 123; //the id of the notification - you can use it to
//change the notification later
PendingIntent pendingNotificationIntent = PendingIntent.getBroadcast(getContext(),mNotificationId,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,timeInMillis,pendingNotificationIntent);
}
Now, you need to create the NotificationReceiver class.
public class NotificationReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
createNotification(); //this is where you create and schedule your notification
}
}
You also need to register the BroadcastReceiver in your AndroidManifext.xml
<receiver
android:name=".util.NotificationReceiver"
android:enabled="true"
android:exported="false" />

Broadcast receiver takes time to receive the broadcast

public synchronized void startAppOnScheduleTime(final int time)
{
//create new calendar instance for your start time
final Calendar startTime= Calendar.getInstance();
//set the time to USER SPECIFIED start time
startTime.set(Calendar.HOUR_OF_DAY, time / 100);
startTime.set(Calendar.MINUTE, time % 100);
startTime.set(Calendar.SECOND, 1); // Add 1 second of additional delay
/*System.out.println(" time set is "+startTime.getTime().toString());
System.out.println(" time set is "+Long.toString(startTime.getTimeInMillis()));
Date date = new Date(startTime.getTimeInMillis());
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss:SSS");
String dateFormatted = formatter.format(date);*/
if (startTime.getTimeInMillis() - Calendar.getInstance().getTimeInMillis() <= 0)
{
// Time has already past, schedule for next day
startTime.add(Calendar.DAY_OF_MONTH, 1);
}
try
{
AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(ALARM_SERVICE);
//create a pending intent to be called at startTime
Intent myIntent=new Intent(getApplicationContext(), StartApplicationReceiver.class);
PendingIntent startPI = PendingIntent.getBroadcast(getApplicationContext(), 0, myIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND), PendingIntent.FLAG_UPDATE_CURRENT);
//schedule time for pending intent, and set the interval to day so that this event will repeat at the selected time every day
am.setRepeating(AlarmManager.RTC_WAKEUP, startTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, startPI);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
//my receiver
public class StartApplicationReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
//on Receive of Start Application Receiver
try
{
//here iam starting an application with the package name
}
}
catch (Exception e)
{
//You don't have the application installed
e.printStackTrace();
}
}
}
I am able to start an application but it starts after two minutes of specified start time and also i have added highest priority for this broadcast in the manifest.
have a look at my manifest
<receiver android:name="packagename.StartApplicationReceiver"
android:priority="999"></receiver>

How to use Alarm Manager to schedule my android app to run in especial time?

I am new with android and I wanna make a alarm app that gets a time from user and make android system to run application in that time.Now I calculate the time between current and the entered time and use the following code to sleep app but doesn't work when my app close or even minimized:
start_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TimePicker tp = (TimePicker) findViewById(R.id.timepicker1);
int hour = tp.getCurrentHour();
int min = tp.getCurrentMinute();
Integer res = min + hour*60;
Calendar c = Calendar.getInstance();
cur_hour = c.get(Calendar.HOUR_OF_DAY);
cur_min = c.get(Calendar.MINUTE);
Integer cur_res = cur_min + cur_hour*60;
if(cur_res > res)
{
res_min =1440-( cur_res - res);
}
else
{
res_min = res-cur_res;
}
res_min *= 60000;
new Handler().postDelayed(new Thread(){
#Override
public void run() {
super.run();
PlayMusic();
}
},res_min);
}
});
I searched hardly for solution and I find out that Alarm Manager is the answer but I couldn't find a proper code to work in this situation.I'm looking for a simple way to perform that.I don't know if there is a better way or not.
Thanks!
Did you try to use the AlarmManager? If you have, post the code that you've tried.
Look at the Google Developer API for classes such as these. They are fantastically documented.
http://developer.android.com/reference/android/app/AlarmManager.html
An example:
//You must set permissions in AndroidManifest.xml
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<receiver android:process=":remote" android:name="Alarm"></receiver>
//In your code
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, Alarm.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), milli, sec, min, pi);

I am trying to set alarm on specific time using alarm manager but alarm initiated instantly?

This is my onclick() function.this will set target alarm
SA=(Button)findViewById(R.id.button1);
SA.setOnClickListener(new OnClickListener() {
#SuppressWarnings("deprecation")
#Override
public void onClick(View v) {
showDialog(id);
}
});
}
#Override
protected Dialog onCreateDialog(int id1) {
switch (id1) {
case id:
// set time picker as current time
return new TimePickerDialog(this,
timePickerListener, hour, min,false);
}
return null;
}
private TimePickerDialog.OnTimeSetListener timePickerListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int selectedHour,
int selectedMinute) {
Calendar calnow=Calendar.getInstance();
calnow.setTimeInMillis(System.currentTimeMillis());
calnow.set(Calendar.HOUR_OF_DAY,selectedHour);
calnow.set(Calendar.MINUTE,selectedMinute);
calnow.set(Calendar.SECOND, 0);
Intent intent=new Intent(getBaseContext(),alarm.class);
PendingIntent pendingintent= PendingIntent.getBroadcast(getBaseContext(),0, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calnow.getTimeInMillis(),pendingintent);
Toast.makeText(getBaseContext(), "alarm set", Toast.LENGTH_SHORT).show();
}
};
public void onReceive(Context arg0, Intent arg1) {
AlarmManager mgr = (AlarmManager)arg0.getSystemService(Context.ALARM_SERVICE);
Toast.makeText(arg0,"Alarm Started.....", Toast.LENGTH_LONG).show();
Here problem is that I get both toasts "alarm set" and"alarm started" as soon as I click button to set alarm before reaching target alarm.
your fault is here. so do it like that
Calendar calnow=Calendar.getInstance()
calnow.set(Calendar.HOUR_OF_DAY,selectedHour);
calnow.set(Calendar.MINUTE,selectedMinute);
calnow.set(Calendar.SECOND, 0);
if (calnow.getTimeInMillis()< System.currentTimeMillis()) {
calnow.set(Calendar.DAY_OF_YEAR, 1)
}
the problem here is, that the value from TP seems to be the current time. It would be helpful if You show more of Your code. Let me give You an example for setting alarm time with a delay of 5 seconds. Please try out this one, it´s a dirty way, I just want to explain. This is what You did:
calnow.set(Calendar.HOUR_OF_DAY,TP.getCurrentHour());
calnow.set(Calendar.MINUTE,TP.getCurrentMinute());
alarmManager.set(AlarmManager.RTC_WAKEUP, calnow.getTimeInMillis(),pendingintent);
to get a delay of five seconds, change it to
alarmManager.set(AlarmManager.RTC_WAKEUP, calnow.getTimeInMillis()+5000,pendingintent);
like I said, this is only to show which value You have to set to alarmManager. It has to be the time in milliseconds, when You want to start the alarm. For this, You have to be sure to get the right values from Your TP. So, if You want us to help You, it will be a good way to show us the complete code

Disabling default AlarmClock alarm

I've set an alarm in the default AlarmClock app. How to disable that previously set alarm? I didn't use AlarmManager for this.
Intent alarm = new Intent(AlarmClock.ACTION_SET_ALARM);
int hour = Integer.parseInt(message.substring(11,13));
int minutes = Integer.parseInt(message.substring(14,16));
if((message.substring(17,19).equalsIgnoreCase("AM") || message.substring(17,19).equalsIgnoreCase("PM")))
{
if(message.substring(17,19).equalsIgnoreCase("AM"))
{
alarm.putExtra(AlarmClock.EXTRA_HOUR, hour);
}
else
alarm.putExtra(AlarmClock.EXTRA_HOUR, hour+12);
alarm.putExtra(AlarmClock.EXTRA_MINUTES, minutes);
alarm.putExtra(AlarmClock.EXTRA_MESSAGE, name+": "+message.substring(20));
}
startActivity(alarm);
In the string message, I'm taking the hour, minutes and AM/PM from the user.
I think you don't need the answer for this question until now but I think some other guys will be need...
Please try :
Intent itentChangeAlarm = new Intent("android.intent.action.ALARM_CHANGED");
alarmChanged.putExtra("alarmSet", enabled);
context.sendBroadcast(itentChangeAlarm);

Categories

Resources