安卓无法设置闹钟时间

发布于 2024-11-13 16:35:25 字数 4749 浏览 2 评论 0原文

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

布局/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难以启齿的温柔 2024-11-20 16:35:25

我能够复制您的问题并找出失败的原因。基本上,假设您像我一样在“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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文