无法设置报警时间在安卓

无法设置报警时间在安卓

本文介绍了无法设置报警时间在安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的闹钟应用程序,我需要时间,从时间选择器,并设置报警经理。但是,未设置我在时间选择器中设置的时间正确的报警管理。所以警报未被在指定的时间激活。我已经包括了我下面的code。任何人可以帮助我呢?

布局/ main.xml中

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    >
<按钮
    机器人:ID =@ + ID / BTN。
    机器人:文本=Alerm
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    />
< / LinearLayout中>
 


MainActivity.java

 公共类MainActivity延伸活动{
    私人按钮BTN = NULL;
    私人AlarmManager alarmManager = NULL;
    日历CAL = Calendar.getInstance();
    静态最终诠释DIALOG_TIME = 0;
    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        alarmManager =(AlarmManager)getSystemService(Context.ALARM_SERVICE);
        BTN =(按钮)findViewById(R.id.btn);
        btn.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                的ShowDialog(DIALOG_TIME);
            }
        });
    }

    @覆盖
    受保护的对话框onCreateDialog(INT ID){
        对话对话框= NULL;
        开关(ID){
        案例DIALOG_TIME:
            对话框=新TimePickerDialog(这一点,新TimePickerDialog.OnTimeSetListener(){

                @覆盖
                公共无效onTimeSet(TimePicker观点,诠释hourOfDay,INT分钟){
                    日历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,分钟);
                    c.set(Calendar.SECOND,0);
                    //c.set(Calendar.MILLISECOND,0);
                    意向意图=新的意图(MainActivity.this,AlarmReceiver.class);
                    PendingIntent PI = PendingIntent.getBroadcast(MainActivity.this,0,意向,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,报警已设置..,Toast.LENGTH_LONG).show();
                }
            },cal.get(Calendar.HOUR_OF_DAY),cal.get(Calendar.MINUTE),FALSE);
            打破;
        }
        返回对话框;
    }
}
 


AlarmReceiver.java

 公共类AlarmReceiver扩展的BroadcastReceiver {
    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        意图I =新的意图(背景下,AlarmActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(ⅰ);
    }
}
 


AlarmActivity.java

 公共类AlarmActivity延伸活动{
    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        新AlertDialog.Builder(AlarmActivity.this).setTitle(任务)。setMessage(时间唤醒)。setPositiveButton(OK,新OnClickListener(){

            @覆盖
            公共无效的onClick(DialogInterface对话,诠释它){
                AlarmActivity.this.finish();
            }
        })创建()显示()。
    }
}
 


的Andr​​oidManifest.xml

 <舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
      包=android.phone
      安卓版code =1
      机器人:VERSIONNAME =1.0>
    <使用-SDK安卓的minSdkVersion =8/>

    <应用机器人:图标=@可绘制/图标机器人:标签=@字符串/ APP_NAME>
        <活动机器人:名称=。MainActivity
                  机器人:标签=@字符串/ APP_NAME>
            <意向滤光器>
                <作用机器人:名称=android.intent.action.MAIN/>
                <类机器人:名称=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
        <活动机器人:名称=。AlarmActivity>< /活性GT;
        <接收机器人:AlarmReceiver名称=安卓程序=:远程>< /接收器>
    < /用途>
< /舱单>
 


解决方案

我能够复制您的问题,并找出它为什么失败。基本上,说你正在测试的12:25像我,然后用12 Calendar.HOUR 和做c.get(Calendar.HOUR_OF_DAY)的返回0。

要更改,只需使用:

 日历C = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis的());
c.set(Calendar.HOUR_OF_DAY,hourOfDay);
c.set(Calendar.MINUTE,分钟);
c.set(Calendar.SECOND,0);
 

即。除去透明,并设置 HOUR_OF_DAY 而不是一小时

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>


解决方案

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

这篇关于无法设置报警时间在安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 04:09