简体   繁体   English

Android警报未触发

[英]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_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);
}

This is called from the main onCreate call. 这是从主要的onCreate调用中调用的。

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? 您是否已将Intent过滤器添加到BroadcastReceiver? Code might look something like this in your AndroidManifest.xml file: 您的AndroidManifest.xml文件中的代码可能类似于以下内容:

    <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. 其中“意图ID文本”可以是您用来识别意图的任何字符串。 Also Android alarms get reset if you reboot your device so that may be something you need to look into. 如果您重新启动设备,Android警报也会重置,因此您可能需要调查一下。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM