安卓无法设置闹钟时间
在我的闹钟应用程序中,我从时间选择器中获取时间并将其设置在闹钟管理器中。但是我在时间选择器中设置的时间在警报管理器中没有正确设置。因此警报不会在指定时间启动。我在下面包含了我的代码。有人可以帮我吗?
布局/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(MainActivity.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>
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(MainActivity.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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能够复制您的问题并找出失败的原因。基本上,假设您像我一样在“12:25”进行测试,然后使用 12 的 Calendar.HOUR 并执行 c.get(Calendar.HOUR_OF_DAY) 返回 0。
要更改,只需使用:
即删除清除,并设置
HOUR_OF_DAY
而不是HOUR
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:
i.e. remove the clear, and set
HOUR_OF_DAY
instead ofHOUR