在Android的静态AlarmManager

编程入门 行业动态 更新时间:2024-10-23 13:26:31
本文介绍了在Android的静态AlarmManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发Android中一个简单的任务的应用程序,我需要通过AlarmManager创建通知。我的问题是,我有一定的报警应删除-and因而其通知 - 但他们都没有,所以我决定-following职位,如Delete使用取消()从AlarmManager报警 - Android电子使AlarmManager一个静态变量,以便同一个实例可以从整个应用程序即可到达。我这样做的方式是有我的主类下面的方法:

I'm developing a simple tasks app in Android and I need to create notifications via an AlarmManager. My problem is that I have certain alarms that should be deleted -and thus their notifications- but they aren't, so I decided -following posts such as Delete alarm from AlarmManager using cancel() - Android to make the AlarmManager a static variable so the same instance can be reached from the whole app. The way I'm doing this is having the following method in my main class:

public static AlarmManager getAlarmManagerInstance() { if (sAlarmManager == null && sContext != null) sAlarmManager = (AlarmManager) sContext .getSystemService(Context.ALARM_SERVICE); return sAlarmManager; }

和在该 sContext 变量将被实例化这种方式:

and in the the sContext variable will be instantiated this way:

@Override protected void onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.activity_main); sContext = this; initActionBar(); }

这是个好主意,创建此变量的单例模式?有没有更好的办法?

Is it a good idea to create a singleton pattern from this variable? Is there any better approach?

非常感谢在前进。

推荐答案

Android文档说:

Android documentation says:

您不要直接实例化这个类;相反,通过Context.getSystemService(Context.ALARM_SERVICE)检索它。

You do not instantiate this class directly; instead, retrieve it through Context.getSystemService(Context.ALARM_SERVICE).

AlarmManager只是提供对系统报警服务的访问类。

AlarmManager is just a class that provides access to the system alarm services.

这个服务在系统中运行,所以不计较他们只需使用AlarmManager作为一个接口来与它们进行交互。

This services are running in the system so don't care about them just use AlarmManager as an interface to interact with them.

所以,每个需要访问该服务的时间作为文件说,刚刚找回它:

So each time that you need to access to this service just retrieve it as the documentation says:

Context.getSystemService(Context.ALARM_SERVICE)

Context.getSystemService(Context.ALARM_SERVICE)

更多推荐

在Android的静态AlarmManager

本文发布于:2023-11-27 09:25:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637502.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:静态   Android   AlarmManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!