0

I'm trying to set up an alarm receiver right after booting. Therefore, i have an OnBootReceiver that should register the alarm. The onBootReceiver works and it gets called, but somehow it cannot find my AlarmReceiver class.

OnBootReceiver which succesfully starts after booting:

public class OnBootReceiver extends BroadcastReceiver {

    private static final String TAG = "OnBootReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "called");

        Intent i = new Intent(context, com.packagenames.AlarmReceiver.class);
        PendingIntent pi = PendingIntent.getService(context, 0, i, 0);
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        Calendar time = Calendar.getInstance();
        time.setTimeInMillis(System.currentTimeMillis());
        time.add(Calendar.SECOND, 30);

        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), time.getTimeInMillis(), pi);

    }

}

As you you can see, it configures the alarm tries to invoke com.packagenames.AlarmReceiver.class. This class exists and is located in the same package:

public class AlarmReceiver extends BroadcastReceiver {

    private static final String TAG = "AlarmReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "alarm received");
        Intent i = new Intent(context, com.packagename.DataService.class);
        i.putExtra("action", "process");
        context.startService(i);
    }

}

Unfortunately, i get the following error:

02-03 09:22:25.344: W/ActivityManager(103): Unable to start service Intent { flg=0x4 cmp=com.phonegap.packagename/.AlarmReceiver (has extras) }: not found

The Android Manifest looks like this

<application>

// activities etc

        <receiver
            android:name="com.phonegap.packagename.OnBootReceiver"
            android:enabled="true"
            android:exported="false"
            android:label="OnBootReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <receiver
            android:name="com.phonegap.packagename.AlarmReceiver"
            android:enabled="true"
            android:label="AlarmReceiver">
            <intent-filter>
            </intent-filter>
        </receiver>

    </application>

Do you see a mistake? Maybe I forgot something?

Thanks

edit: in the manifest, I added

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

in order to make the OnBootReceiver work. Do I need something similar for the alarm?

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
ceran
  • 1,392
  • 1
  • 17
  • 43
  • You shouldn't have to add anything for your Alarm. – JoxTraex Feb 03 '12 at 08:50
  • [http://stackoverflow.com/questions/4459058/alarm-manager-example/8801990#8801990][1] [1]: http://stackoverflow.com/questions/4459058/alarm-manager-example/8801990#8801990 – XXX Feb 03 '12 at 08:54

2 Answers2

1

Shouldn't you use getBroadcast instead of getService when creating a pending intent ?

Snicolas
  • 37,840
  • 15
  • 114
  • 173
0

The whole receiver stuff ONLY works if you application is NOT installed on a SD card. Add this to your manifest file to do so:

android:installLocation="internalOnly"

Dominik
  • 1,703
  • 6
  • 26
  • 46