I\'m trying to build a personal app that will set alarms in the DeskClock app. I can get it to set alarms for anytime in the current day, But how would I go about setting an
MainActivity.java
public class MainActivity extends Activity {
TimePicker myTimePicker;
Button buttonstartSetDialog;
TextView textAlarmPrompt;
TimePickerDialog timePickerDialog;
final static int RQS_1 = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textAlarmPrompt = (TextView) findViewById(R.id.alarmprompt);
buttonstartSetDialog = (Button) findViewById(R.id.startAlaram);
buttonstartSetDialog.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
textAlarmPrompt.setText("");
openTimePickerDialog(false);
}
});
}
private void openTimePickerDialog(boolean is24r) {
Calendar calendar = Calendar.getInstance();
timePickerDialog = new TimePickerDialog(MainActivity.this,
onTimeSetListener, calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE), is24r);
timePickerDialog.setTitle("Set Alarm Time");
timePickerDialog.show();
}
OnTimeSetListener onTimeSetListener = new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar calNow = Calendar.getInstance();
Calendar calSet = (Calendar) calNow.clone();
calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
calSet.set(Calendar.MINUTE, minute);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);
if (calSet.compareTo(calNow) <= 0) {
// Today Set time passed, count to tomorrow
calSet.add(Calendar.DATE, 1);
}
setAlarm(calSet);
}
};
private void setAlarm(Calendar targetCal) {
textAlarmPrompt.setText("\n\n***\n" + "Alarm is set "
+ targetCal.getTime() + "\n" + "***\n");
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getBaseContext(), RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),
pendingIntent);
}
}
Reciver.java
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context k1, Intent k2) {
// TODO Auto-generated method stub
Toast.makeText(k1, "Alarm received!", Toast.LENGTH_LONG).show();
}
}
main_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" >
<Button
android:id="@+id/startAlaram"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Set Alaram Time" />
<TextView
android:id="@+id/alarmprompt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000" />
</LinearLayout>
Manifest.xml
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".AlarmReceiver" android:process=":remote" />
</application>
How about trying a pending Intent?
Just change the Calendar value to a few days or so in advance?
AlarmManager am = (AlarmManager)getSystemService(alarm);
Intent i= new Intent("MY_INTENT");
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.MINUTE, 2);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi);
Like a real alarm clock, I don't see a way to set an alarm for a future date. I think this requires one of two options:
A combination of AlarmManager (to trigger an app service on the desired future date to create the alarm in AlarmClock) and then AlarmClock (to actually handle the alarm). Don't forget to listen for BOOT_COMPLETED broadcast to reset the AlarmManager alarms since they don't persist after a reboot.
Use AlarmManager to trigger an app service and UI that simulates an alarm. This will require you to consider phone state and etc things you don't have to worry about with AlarmClock.
I think you can make your app just bring up the AlarmClock dialog which allows the user to set recurring alarms and other options you cannot perform through the intent.