睡眠 VS alarmManager.set VS alarmManager.set重复 VS 处理程序

Sleep VS alarmManager.set VS alarmManager.setRepeat VS Handler

我正在寻找执行简单任务的最有效方法。作为一名新的 android 开发人员,我不太确定这些策略中的哪一个在内存效率方面最适合我的应用程序。我想其中一些方法可能会导致我不知道的线程问题。

所有三个解决方案目前都按预期运行。

这是一个非常简单的应用程序。我的想法是我的 MainActivity 启动一个 IntentService ,在应用程序打开后,它将在后台 运行 。我现在需要的所有功能都是在一天中以随机间隔(大约相隔一个小时)无限期地创建通知,直到被用户停止。通知是在一个简单的 void 方法中进行的,将通知显示为文本并振动 phone 一次。

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

请注意,用户必须卸载该应用才能停止通知,这是不可取的(尽管我认为我可以添加一个按钮或其他可以取消意图的东西)

方法二:

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

谁能帮我决定这三种方法的pros/cons?我不确定我是否理解哪一个是理想的,尽管它们三个都为我提供了适当的功能。作为旁注,在我的研究中我注意到 "Handler" class 在这里也可能有用。

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() 时 运行ning(如果快速连续向其发送 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()) 还是不定期发生的警报 (set())。

如果选择 setRepeating() 选项,请将 AlarmManager 代码移出服务并移至 activity。在每个警报上调用 setRepeating() 没有意义——而且有一定的成本。毕竟setRepeating()背后的重点是它知道要重复,所以你不需要在每次出现时都告诉它"oh, hey, I know that I told you the last 1,337 times that you should repeat, but, um, don't forget to repeat, m'kay?"。

使用 set() 选项,因为您明确地 不是 要求重复警报,您将继续在服务中安排它们(或者可能一次从activity,然后是服务的其余部分),或多或少与您拥有的一样。