【问题标题】:How do I configure my AlarmReceiver to actually trigger at my desired interval?如何配置我的 AlarmReceiver 以在我想要的时间间隔实际触发?
【发布时间】:2015-06-02 22:25:52
【问题描述】:

我的应用程序这部分的目标是始终在后台运行重复警报,每 15 分钟从服务器端机器学习算法中获取一个新预测,更新应用程序。

我目前已经实现了这种所需行为的框架,以确保我的方法正确。这个骨架应该每 10 秒触发一次祝酒,说明警报正在工作。但是,在我最初设置闹钟后,我再也没有看到其他消息。我还向控制台写入了一条消息,但这也从未出现过,这让我相信我并不完全了解警报接收器的工作原理。

这是我实例化警报和接收器的主要活动类:

public class MainActivity extends AppCompatActivity implements
        TimePickerFragment.FragmentCallbacks {

    private PendingIntent pendingIntent;
    private AlarmManager manager;

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

        // Retrieve a PendingIntent that will perform a broadcast
        Intent alarmIntent = new Intent(this, PredictionUpdateReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
        startAlarm();

        //...
    }

    //...

    public void startAlarm() {
        manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        int interval = 10000;

        manager.setRepeating(AlarmManager.RTC_WAKEUP,
                System.currentTimeMillis(), interval, pendingIntent);
        Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
    }
}

这是我的报警接收器类:

public class PredictionUpdateReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // For our recurring task, we'll just display a message
        Toast.makeText(arg0, "I'm running", Toast.LENGTH_SHORT).show();
        System.out.print("Alarm activated");

    }

}

我已经更新了我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.habitabilitystudy"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="22" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver android:name=".PredictionUpdateReceiver"></receiver>
        <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>
    </application>

</manifest>

【问题讨论】:

  • 你在哪个版本的 Android 上运行这个?
  • 我正在运行一个 Galaxy nexus 模拟器,5.1.1 API 级别 22

标签: android android-intent alarmmanager android-alarms repeatingalarm


【解决方案1】:

嗯,你有几个问题。

首先,setRepeating() 在 Android 4.4 及更高版本上不精确,targetSdkVersion 为 19 或更高版本。他们没有说明它实际上有多“不精确”,所以我不知道你的闹钟什么时候会安排好。

第二,a repeating alarm period of less than one minute is not allowed on Android 5.1+,由于未记录的回归。一分钟以下的时段将四舍五入为一分钟。所以,不管不准确,你都不会在 10 秒内得到控制。

最后,changes coming in the next version of Android 表示当设备空闲(且未充电)或用户长时间未返回您的应用(而设备处于不收费)。这对你现在没有影响,但你可能想考虑一下你对你正在做的事情的态度是否会继续发挥作用。

您可以使用 adb shell dumpsys alarm 查看您的闹钟是否已安排。不幸的是,除非他们修复了问题,否则由于不准确的问题,它不会告诉您警报何时实际运行。

另外,我建议使用Log.d(),或者最糟糕的是System.out.println(),而不是System.out.print()

【讨论】:

  • 谢谢,有没有更好的方法让我在后台抓取这些经期预测调整?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多