0

所以,我似乎掉进了一个兔子洞,试图找出通知用户警报响起的最佳方法。

基本上,我希望在某个时间出现某种通知/对话框,无论用户在做什么,它都应该出现,并阻止进一步使用,直到采取行动(被解雇或以其他方式)。

现在,我有一个 AlarmManager,向它注册的 BroadcastReceiver 启动了一个新服务。

每次我认为自己朝着正确的方向前进时,我都会遇到网上有人遇到类似问题的问题,并被告知“不要那样做”。(例如,让服务创建/显示一个 AlertDialog。)

我希望有人能给我一份简短的清单,说明他们的建议是什么;我不需要代码(至少我不应该),只需要一些高级抽象。

4

3 回答 3

0

Go with Notification, which plays a sound perhaps, that would pull your user's attention to your notification, just like the default alarm does.

And make the notification an ongoing one. Which can't be removed by the user, until and unless some action is performed to change the state of the notification.

Android: How to create an "Ongoing" notification?

Dialogs for this situation would be annoying for me. The docs also suggest not to use them in these scenarios.

于 2013-06-12T05:53:15.337 回答
0

看看这个示例开源项目

于 2013-06-12T07:01:11.940 回答
0

我以这种方式做到了,对我来说效果很好。

创建一个类并将其称为ScheduledService它扩展了 IntentService,在这个类中,您将在警报响起时做您想做的事情。

public class ScheduledService extends IntentService {

public ScheduledService() {
    super("My service");
}

@Override
protected void onHandleIntent(Intent intent) {
    //Do something, fire a notification or whatever you want to do here
    Log.d("debug", "Ring Rind !");

}
}

然后在您的活动中启动警报使用以下内容:

AlarmManager mgr = (AlarmManager) YourActivity.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(YourActivity, ScheduledService.class);
PendingIntent pi = PendingIntent.getService(YourActivity, 0, i, 0);
mgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + PERIOD, pi);

哪个PERIOD是您希望警报响起的毫秒数。

要取消停止计时器并取消闹钟,请使用:

if (mgr != null)
        mgr.cancel(pi);

最后,要使所有这些工作,您需要将ScheduledService类注册为服务。在你的清单中添加这个 tou 你的应用程序:

<application
    ... />
    ...
    <service android:name=".ScheduledService" >
    </service>

</application>

这样,Android 操作系统将负责在时间到时触发警报。即使其他应用程序正在运行,或者即使您的应用程序进程已终止。

希望这有帮助。问候。

只是一个疯狂的想法:创建一个活动并将其主题设置为全屏,没有标题栏和一个停止警报的按钮,而不是做一个通知,只是做一个启动该活动的意图“也许你需要这个”来工作即使手机被锁定并播放一些烦人的声音,当活动开始时,“ This ”也可能会有所帮助。你也可以重写onBackPressed()什么都不做。

于 2013-06-12T06:13:30.947 回答