Schedule an alarm on device reboot automatically in android

戏子无情 提交于 2019-12-11 16:43:20

问题


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.

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);
    }


}

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 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>

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

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


回答1:


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.



来源:https://stackoverflow.com/questions/51290935/schedule-an-alarm-on-device-reboot-automatically-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!