Unable to set alarm time in android
In my alarm application, I take time from time picker and set it in alarm manager. But the time which I set in the time picker was not set properly in alarm manager. So the alarm is not activated at the specified time. I have included my code below. Could anybody help me out?
layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/btn"
android:text="Alerm"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
private Button btn = null;
private AlarmManager alarmManager = null;
Calendar cal = Calendar.getInstance();
static final int DIALOG_TIME = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DIALOG_TIME);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
switch(id){
case DIALOG_TIME:
dialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
c.clear();
//c.set(Calendar.YEAR, 2011);
//c.set(Calendar.MONTH, 6);
//c.set(Calendar.DAY_OF_MONTH, 4);
c.set(Calendar.HOUR, hourOfDay);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, 0);
//c.set(Calendar.MILLISECOND, 0);
Intent intent = new Intent(MainActivity.this,AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
//alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pi);
Toast.makeText(MainA开发者_运维技巧ctivity.this, "Alarm has been set..", Toast.LENGTH_LONG).show();
}
},cal.get(Calendar.HOUR_OF_DAY),cal.get(Calendar.MINUTE),false);
break;
}
return dialog;
}
}
AlarmReceiver.java
public class AlarmReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, AlarmActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
AlarmActivity.java
public class AlarmActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new AlertDialog.Builder(AlarmActivity.this).setTitle("Task").setMessage("Time to wake up").setPositiveButton("ok", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
AlarmActivity.this.finish();
}
}).create().show();
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.phone"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AlarmActivity"></activity>
<receiver android:name=".AlarmReceiver" android:process=":remote"></receiver>
</application>
</manifest>
I was able to replicate your problem and find why it was failing. Basically, say you were testing at "12:25" like i was, then using Calendar.HOUR
of 12 and doing c.get(Calendar.HOUR_OF_DAY) was returning 0.
To change, simply use:
Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
c.set(Calendar.HOUR_OF_DAY, hourOfDay);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, 0);
i.e. remove the clear, and set HOUR_OF_DAY
instead of HOUR
精彩评论