Android alarm并非每天都正常工作

Android alarm并非每天都正常工作,android,alarm,Android,Alarm,我编写了下面的代码,每天在9.0和3.30进行广播启动服务。但它总是只运行一次。请让我知道我做错了什么。先谢谢你。 我使用首选项检查报警是否已设置,然后它跳过。这是为了避免应用程序每次运行时都设置报警 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

我编写了下面的代码,每天在9.0和3.30进行广播启动服务。但它总是只运行一次。请让我知道我做错了什么。先谢谢你。 我使用首选项检查报警是否已设置,然后它跳过。这是为了避免应用程序每次运行时都设置报警

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        @SuppressWarnings("unused")
        DatabaseHandler db = new DatabaseHandler(getApplicationContext());

        pref2 = getSharedPreferences(PREFS_N, MODE_PRIVATE);
        hh = pref2.getString(PREFS_USER, Da);

        if (hh.equals(Da)) {
            new update().execute();
        } else {
            Log.i("ssssssssssssssssss", hh);

            dates = (TextView) findViewById(R.id.date);
            buy = (TextView) findViewById(R.id.fbuying_rate);
            sell = (TextView) findViewById(R.id.fselling_rate);
            Ebuy = (TextView) findViewById(R.id.ebuying_rate);
            Esell = (TextView) findViewById(R.id.eselling_rate);

            buy.setText(pref2.getString(PREFS_FBUY, ""));
            sell.setText(pref2.getString(PREFS_FSELL, ""));
            Ebuy.setText(pref2.getString(PREFS_EBUY, ""));
            Esell.setText(pref2.getString(PREFS_ESELL, ""));
            dates.setText(pref2.getString(PREFS_DATE, ""));

        }
        pref = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);

        kk = pref.getString(PREFS_USERNAME, De);

        Log.i("gasf", kk);
        if (kk.equals(De))

        {

            Calendar updateTime = Calendar.getInstance();
            updateTime.set(Calendar.HOUR_OF_DAY, 9);
            updateTime.set(Calendar.MINUTE, 30);

            Intent alarm = new Intent(this, AlarmReceiver.class);
            int i = 23;
            AI = PendingIntent.getBroadcast(this, i, alarm, PendingIntent.FLAG_CANCEL_CURRENT);
            am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            am.setInexactRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, AI);


            Calendar updateTime2 = Calendar.getInstance();
            updateTime2.set(Calendar.HOUR_OF_DAY, 15);
            updateTime2.set(Calendar.MINUTE, 30);

            Intent alarm2 = new Intent(this, AlarmReceiver.class);
            int i2 = 32;
            AI2 = PendingIntent.getBroadcast(this, i2, alarm2, PendingIntent.FLAG_CANCEL_CURRENT);
            am2 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            am2.setInexactRepeating(AlarmManager.RTC_WAKEUP, updateTime2.getTimeInMillis(), AlarmManager.INTERVAL_DAY, AI2);


            getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit().putString(PREFS_USERNAME, "lol").commit();
        }
        lvCustomList = (ListView) findViewById(R.id.lv_custom_list);
        showList();

    }

am2.setInexactRepeating
就是这样!这不是自我解释吗?您需要setExact()/setRepeating()(取决于您的用例)-有关更多信息,请查看我的其他答案

setInexactRepeating是一款操作系统和电池友好型产品,它将接收警报时需要完成的所有工作分批处理在一起,一个接一个地工作,而as setRepeating会立即触发警报
还有一个注意事项:一旦手机重新启动,警报就会被清除,您可能需要实现一个引导广播接收器,使其持久化。请确保您没有执行该运行时,您需要在清单中实现它,否则当您的应用程序不在后台时,您将不会收到任何广播。

请参阅:嘿,我使用setRepeating()和AI=PendingEvent.getBroadcast尝试了相同的代码(此,I,alarm,PendingEvent.FLAG\u UPDATE\u CURRENT);尽管如此,它对我还是不起作用。请参阅API 19及以上警报工作原理的文档。您需要手动管理它。还提供一些关于您的测试环境的见解。将目标设置为api 23。min api 8。正如您在我的代码中看到的,您认为我写的有什么错误吗?请对照我在回答中链接的代码进行检查-这是您可以使用的生产级代码。还要更新问题中的代码,以反映您所做的更改。