开发者

Android alarm not being triggered

I"m sure this is something that is simple, but I'm not figuring it out. I'm trying to make a simple repeating alarm and it never gets triggered. What I have is:

private void setupAlarms()
{
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVIC开发者_开发知识库E);

    Intent intent = new Intent(this, RepeatingAlarm.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(HelloAndroid.this, 0, intent, 0);

    GregorianCalendar fifteenSeconds = (GregorianCalendar)Calendar.getInstance();
    fifteenSeconds.add(Calendar.MINUTE, 0);
    fifteenSeconds.set(Calendar.SECOND, 15);

    alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime(), fifteenSeconds.getTimeInMillis(), pendingIntent);
}

This is called from the main onCreate call.

My alarm receiver:

public class RepeatingAlarm extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Toast.makeText(context, R.string.hello, Toast.LENGTH_SHORT).show();
    }
}

In my manifest, I have added:

<receiver android:name=".RepeatingAlarm" android:process=":remote" />

Any help, much appreciated


Have you added an intent filter to your BroadcastReceiver? Code might look something like this in your AndroidManifest.xml file:

    <receiver android:name=".RepeatingAlarm" android:exported="true">
        <intent-filter>
            <action android:name="intent id text" />
        </intent-filter>
    </receiver>

and when creating your intent do something like this:

Intent intent = new Intent("intent id text")

where the "intent id text" can be any string you use to identify your intent. Also Android alarms get reset if you reboot your device so that may be something you need to look into.

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜