Search code examples
androidbroadcastreceiveralarmmanager

Starting alarm to call IntentService at boot time


I'm trying to set an alarm at boot time, and have it call an IntentService periodically via the AlarmManager. The AlarmReceiver is never triggered, not sure why.

BootReceiver:

package com.company.android;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.text.format.Time;

public class BootBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Time now = new Time();
        now.setToNow();

        Log.e("#######################COMM", "Booting up " + now.toString());

        Intent alarmIntent = new Intent("com.company.android.AlarmReceiver");
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis(), 1000 * 5, pi);

        Log.e("#######################COMM", "Booted up " + now.toString());
    }
}

AlarmReceiver:

package com.company.android;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by mlaino on 7/8/13.
 */
public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("COMM", "Pre");


        Intent i = new Intent("com.company.android.PollingService");
        context.startService(i);

        Log.d("COMM","Pos");

    }
}

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.company.android" android:versionCode="1" android:versionName="1.0-SNAPSHOT">
    <uses-sdk
            android:minSdkVersion="14"
            android:targetSdkVersion="15" />
  <application android:icon="@drawable/icon" android:label="@string/app_name">
    <receiver android:name=".BootBroadcastReceiver"
            android:enabled="true"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>
      <receiver android:name=".AlarmReceiver">
          <intent-filter>
              <action android:name="com.company.android.AlarmReceiver" />
          </intent-filter>
      </receiver>

      <activity android:name=".CommunicationsManagerActivity">
          <intent-filter>
              <action android:name="android.intent.action.MAIN" />
              <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>
      </activity>
      <service android:name=".PollingService" >
          <intent-filter>
              <action android:name="com.company.android.PollingService" />
          </intent-filter>
      </service>
  </application>

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

Any clues?

Thanks


Solution

  • First, remove android:permission="android.permission.RECEIVE_BOOT_COMPLETED", as you are saying that whatever is calling your receiver must hold that permission, which may or may not be the case.

    Then, be sure to run one of your activities before rebooting, as manifest-registered BraodcastReceivers will not work until something explicitly runs one of your components, typically accomplished by the user launching an activity.