无法在android中设置闹钟时间

时间:2011-06-04 10:09:51

标签: android android-alarms

在我的闹钟应用程序中,我从时间选择器中抽出时间并将其设置在闹钟管理器中。但是我在时间选择器中设置的时间没有在报警管理器中正确设置。因此,警报不会在指定时间激活。我在下面提供了我的代码。有人可以帮帮我吗?

布局/ 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>

1 个答案:

答案 0 :(得分:3)

我能够复制你的问题并找出失败的原因。基本上,假设你像我一样在“12:25”进行测试,然后使用12的Calendar.HOUR并且执行c.get(Calendar.HOUR_OF_DAY)返回0。

要更改,只需使用:

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);

即。删除明文,然后设置HOUR_OF_DAY而不是HOUR