【问题标题】:Android alarm is not working everyday as it shouldAndroid 闹钟无法正常工作
【发布时间】:2015-12-04 04:20:01
【问题描述】:

我写了下面的代码,每天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();

    }

【问题讨论】:

标签: android alarm


【解决方案1】:

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

setInexactRepeating,对操作系统和电池友好,它将所有要在警报接收上完成的工作分批处理并一一完成,而 setRepeating 会立即触发警报 另请注意:一旦手机重新启动,警报就会消失,您可能必须实现启动广播接收器以使其持久化。确保您不执行该运行时,您需要在 Manifest 中实现它,否则当您的应用不在后台时,您将不会收到任何广播。

【讨论】:

  • 嘿,我确实用 setRepeating() 尝试了相同的代码,并且 AI = PendingIntent.getBroadcast(this, i, alarm, PendingIntent.FLAG_UPDATE_CURRENT);仍然对我不起作用
  • 请参阅有关警报如何在 API 19 及以上版本上工作的文档。您需要手动管理。还提供有关您的测试环境的一些见解。
  • 我正在测试 API 17。目标设置为 api 23。最小 api 8。正如您在我的代码中看到的,您认为我写错了什么吗?
  • 请检查我在答案中链接的代码 - 这是您可以使用的生产级代码。同时更新问题中的代码以反映您所做的更改。
猜你喜欢
  • 2017-02-11
  • 1970-01-01
  • 2016-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-10
  • 2012-09-13
  • 2012-03-24
相关资源
最近更新 更多