在Android中自动重新启动设备时安排警报 [英] Schedule an alarm on device reboot automatically in android

查看:153
本文介绍了在Android中自动重新启动设备时安排警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个应用程序,该应用程序每小时检查一次服务器上的通知文件。我使用了警报管理器类来实现这一点。但是我无法在重新启动部分实现自动启动。我希望警报在重新启动后应定期运行。有人可以告诉我如何去做吗。

I am trying to build an application which checks for notification files on a server, every hour. I used the alarm manager class to implement this. But I am unable to implement the automatic start on reboot part. I want that the alarm should run periodically after reboot. Can some one please tell me how to go about doing it.

这是我的MyAlarmReceiver类。

This is my MyAlarmReceiver Class.

package com.example.quickstart;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

public class MyAlarmReceiver extends BroadcastReceiver {
    public static final int REQUEST_CODE = 12345;
    public static final String ACTION = "com.example.quickstart.alarm";

    // Triggered by the Alarm periodically (starts the service to run task)
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, NotificationService.class);
      //  i.putExtra("username", username);
        context.startService(i);
    }


}

这是我的NotificationBootReceiver类。

This is my NotificationBootReceiver Class.

package com.example.quickstart;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;

public class NotificationBootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

            // Construct an intent that will execute the AlarmReceiver
            Intent i = new Intent(context, MyAlarmReceiver.class);
            // Create a PendingIntent to be triggered when the alarm goes off
            final PendingIntent pIntent = PendingIntent.getBroadcast(context, MyAlarmReceiver.REQUEST_CODE,
                    i, PendingIntent.FLAG_UPDATE_CURRENT);
            // Setup periodic alarm every every half hour from this point onwards
            long firstMillis = System.currentTimeMillis(); // alarm is set right away
            AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            // First parameter is the type: ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC_WAKEUP
            // Interval can be INTERVAL_FIFTEEN_MINUTES, INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_DAY
            alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),
                    2*60*60,pIntent);
            // alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis,
            //    AlarmManager.INTERVAL_FIFTEEN_MINUTES, pIntent);

    }
}

AndroidManifest文件如下所示(不是完整的清单文件,而是相关部分)

AndroidManifest file looks like this(it's not the complete manifest file but the relevant parts)

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<receiver android:name=".NotificationBootReceiver"
            android:enabled="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"></action>
            </intent-filter>
        </receiver>

通知工作正常,但是我想要的是重新启动时自动启动通知功能。当我重新启动时,通知功能不起作用,即,警报不会响起。并且android文档还说 (这仅在应用程序已经被用户至少启动过一次的情况下有效) ,有没有办法在重启时自动设置警报而不启动应用程序。任何帮助表示赞赏。预先感谢

The notifications are working fine, but what I want is the start notifications automatically on reboot feature. When i reboot the notification feature does not work,i.e, the alarm does not go off. And also the android documentation says (this only works if the app has already been launched by the user at least once) , is there a way to set the alarm automatically on reboot without launching the app. Any help appreciated. Thanks in advance

文档链接 android devs链接
(转到:设备重新启动时启动警报)

Documentation link android devs link (Go to: Start an alarm when the device restarts)

推荐答案

不是吗?甚至在没有启动应用程序的情况下也无法设置警报。一旦启动应用程序,便可以触发警报。

No it isn't possible to set the alarms without starting the app even once. Once the app has been started the alarms can be triggered.

这篇关于在Android中自动重新启动设备时安排警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆