0

我确定这很简单,但我没有弄清楚。我正在尝试制作一个简单的重复警报,但它永远不会被触发。我所拥有的是:

private void setupAlarms()
{
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

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

这是从主 onCreate 调用中调用的。

我的报警接收器:

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

在我的清单中,我添加了:

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

任何帮助,非常感谢

4

1 回答 1

0

您是否在 BroadcastReceiver 中添加了意图过滤器?AndroidManifest.xml 文件中的代码可能如下所示:

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

并在创建您的意图时执行以下操作:

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

其中“intent id text”可以是您用来识别您的意图的任何字符串。如果您重新启动设备,Android 警报也会重置,这可能是您需要查看的内容。

希望这可以帮助。

于 2011-01-20T13:42:45.880 回答