- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在寻找执行简单任务的最有效方法。作为一名新的 android 开发人员,我不太确定这些策略中的哪一个在内存效率方面最适合我的应用程序。我想其中一些方法可能会导致我不知道的线程问题。
所有三个解决方案目前都按预期运行。
这是一个非常简单的应用程序。这个想法是我的 MainActivity 启动一个 IntentService ,它将在应用程序打开后在后台运行。我现在需要的所有功能都是在一天中以随机间隔(大约相隔一个小时)无限期地创建通知,直到被用户停止。通知是在一个简单的 void 方法中进行的,将通知显示为文本并使手机振动一次。
我的 MainActivity 启动 IntentService:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, NotificationService.class);
startService(intent);
}
}
我的 IntentService 非常简单。它被称为 NotificationService,扩展了 IntentService,并且只重写了 onHandleIntent 方法。除了 super("Service") 之外,构造函数是空的。问题在于如何以最有效的方式在后台全天弹出通知。在我的实现中,这是在所有三种方法的 onHandleIntent 方法中完成的。
方法一:
@Override
protected void onHandleIntent(Intent intent) {
makeNotification();
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
PendingIntent pintent = PendingIntent.getService(
getApplicationContext(), 0, intent, 0);
alarm.cancel(pintent);
alarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()
+ 60000 * 60, pintent);
}
请注意,有了这个,用户必须卸载应用程序才能停止通知,这是不可取的(尽管我认为我可以只添加一个按钮或其他可以取消 Intent 的东西)
方法二:
protected void onHandleIntent(Intent intent) {
makeNotification();
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
PendingIntent pintent = PendingIntent.getService(
getApplicationContext(), 0, intent, 0);
alarm.cancel(pintent);
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()
+ 60*1000, 60000*60 ,pintent);
}
方法三:
@Override
protected void onHandleIntent(Intent intent) {
makeNotification();
try {
sleep(60000 * 60);
startService(intent);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
有人可以帮我确定这三种方法的优缺点吗?我不确定我是否理解哪一个是理想的,尽管它们三个都为我提供了适当的功能。作为旁注,在我的研究中我注意到一个“处理程序”类,它在这里也可能有用。
最佳答案
All the functionality I need right now is for a notification to be created at random intervals throughout the day(about an hour apart), indefinitely, until stopped by the user.
AlarmManager
和潜在的 JobScheduler
是唯一可行的选择。
The idea is that my MainActivity starts an IntentService which will be running in the background after the app is opened
不是真的。 IntentService
只会在完成 onHandleIntent()
时运行(如果快速连续向其发送 N 个命令,则包括执行 N 次)。 IntentService
可以运行一段时间,但它旨在处理某种业务逻辑事务。它不是设计为无限期运行,无论如何这样做对用户都是不利的。
Can someone please help me with deciding the pros/cons of these three methods?
选项三不可用。首先,它不可靠,因为一旦您的进程终止,它就会停止工作。其次,它无缘无故占用了大量系统 RAM,用户可以将 RAM 用于更高效的使用。 Only have a service running when it is actively delivering value to the user .看着时钟滴答作响并没有积极地向用户传递值。
I've noticed a "Handler" class which may also be useful here
不,因为它会遇到与选项三相同的问题。
关于您的两个 AlarmManager
选项,它归结为您是想要定期发生的警报 (setRepeating()
) 还是不规则发生的警报 (设置()
).
如果您选择 setRepeating()
选项,请将 AlarmManager
代码移出服务并移至 Activity 中。在每个警报上调用 setRepeating()
没有意义,而且成本肯定很高。毕竟,setRepeating()
背后的要点是它知道要重复,所以你不需要在每次出现时都告诉它“哦,嘿,我知道我告诉你最后 1,337 次你应该重复,但是,嗯,不要忘记重复,好吗?
使用 set()
选项,由于您明确不要求重复警报,您将继续在服务中安排它们(或者可能一次从 Activity ,然后是服务中的其余部分),或多或少取决于您的情况。
关于java - sleep VS alarmManager.set AND alarmManager.setRepeating VS 处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40197820/
我在 linux (Centos) 上使用 pthread 编程?我想让线程休眠一小段时间以等待某些事情。我正在尝试使用 sleep()、nanosleep() 或 usleep() 或其他可以做到这
此要求出现在我的 Android 应用程序中,但它通常适用于 Java。我的应用程序每隔几秒钟“做某事”。我已经按如下方式实现了这一点(只是相关的 fragment - 不是完整的代码): fragm
我正在使用 esp8266 构建 IR 到 WiFi 桥接器。基本上,我正在构建一个连接到红外 Remote 内的 esp8266 的红外接收器,以通过 wifi 将接收到的红外远程按键转发到服务器。
我想让 Toast 出现,然后让 sleep 运行。 如果我这样做,Toast 会在 sleep 后出现,但我希望反过来。有人有建议吗?这是我的代码 switch (checkedRadioButto
我在做一件简单的事情,打印一条错误消息,延迟执行 5 秒,然后调用另一个函数,这是代码 public void saveAndDisplay() throws InterruptedException
我的 Activity 上有一个按钮,当我单击它时,我希望按钮改变颜色,等待一段时间,然后再次改变颜色。 我尝试过以下两个版本的 sleep : 尝试1: public void buClick(Vi
我尝试过这个: for(int i =0; i1。创建ScheduledExecutorService public static ScheduledExecutorService createSch
我有一个 Winform,需要等待大约 3 - 4 小时。我无法关闭并以某种方式重新打开应用程序,因为它在等待时在后台做的事情很少。 为了实现等待 - 不给 UI 线程造成麻烦和其他原因 - 我有一个
我在网上看到了下面一段关于 Linux 线程的代码。但是当我运行它时,所有线程似乎都在 sleep ,而不仅仅是主线程。为什么?另外,如果没有 sleep(5),“线程创建成功”语句会运行 3 次而不
我有一个 php 脚本,我需要每 5 秒运行一次(运行,等待它完成,等待 5 秒,再次运行) 我有两种方法。要么在脚本中有一个带有 sleep 功能的无限循环,看起来像这样: while (1) {
我有一个图形用户界面,我想显示一些文本,然后稍等一下。 我的代码看起来像这样: //do something (add JTextArea, revalidate, repaint) try{
我想知道安卓手机的 sleep 模式和深度 sleep 模式有什么区别,手机进入休眠模式和深度 sleep 模式会有什么影响。 请提供详细的答案。 问候,皮克斯 最佳答案 深度 sleep 模式与休眠
我正在学习 C。在这个节目中我使用 sleep 功能来减慢倒计时。我的教科书没有指定我应该包含的库来使用 sleep 功能。所以我使用它时没有为它包含任何特殊的库并且它可以工作。但它在代码块中给了我这
我正在尝试模拟按键按下和按键 Action 。 例如:2638 毫秒。 SendMessage(hWnd, WM_KEYDOWN, keyCode, 0); Sleep(2638); SendMess
我在 while 循环中调用一个线程 hibernate 1 秒。当标志为真时,循环将运行(标志为真无限时间)。在循环线程内应该 hibernate 1 秒,唤醒并增加计数器,检查 IF 条件,在 F
有很多文件说“你应该避免使用带警报的 sleep ,因为许多系统使用警报来实现 sleep ”。实际上,我正在为这个问题而苦恼。 那么,当 sleep() 不能很好地处理警报时,是否有人可以帮助我“
我有两个带有图像的jlabel..我尝试在单击另一个标签时更改标签中的图像..(例如游戏)..我有一个问题..当我编写 Thread.sleep 时,图像没有改变..请参阅代码: public cla
我正在研究多线程,我有一个关于线程 sleep 方法的问题。当我在已经处于 sleep 线程(时间 t2)中执行 sleep()(时间 t1)方法时。总 sleep 时间为 t1+t2 或 t2(如果
如果我们不向 sleep( ) 函数传递任何参数,默认 sleep 时间是多少? #include int main() { int pid,dip,cpid; pid = fork(
当我转到 asyncio 页面时,第一个示例是一个 hello world 程序。当我在 python 3.73 上运行它时,我看不出与正常的有什么不同。 谁能告诉我区别并举一个重要的例子? In [
我是一名优秀的程序员,十分优秀!