我应该为我的应用创建 5 个不同的 receivers/services 吗?

Should I create 5 different receivers/services for my app?

我刚开始在学校和工作之外申请,不知道我应该走哪条路。本质上,我正在构建一个应用程序来计算每天变化的 5 个不同的祈祷时间(这 5 个祈祷被命名为 Fajr、Zuhr、Asr、Maghrib 和 Isha)。计算是在设备本地完成的,我找到了开源代码并让它正确计算它们。调用 getTimes() 方法后,应计算当天的祈祷时间,然后每天重新计算一次。我认为 AlarmManager class 的 setRepeating() 方法对此有好处。我该怎么做?计算出祈祷时间后,应启动服务以在该确切时间创建通知,以通知用户该祈祷了。这里的困境是我认为我不应该使用 5 个不同的 services/receivers 来通知 5 个不同的祈祷中的每一个。解决此问题的最佳方法是什么?

目前,我的应用程序只通知用户 Maghrib(祈祷之一)祈祷时间。它也不会重新计算时间。

对不起,如果我不是很清楚,因为我是新手。如果需要我可以扩展更多。

我的getTimes()方法:(为了简单起见,我删除了计算时间的代码)

public void getLocationTime(View v) {

        //Maghrib
        Calendar calMaghribTime = Calendar.getInstance();
        calMaghribTime.set(Calendar.HOUR_OF_DAY, getHourOfDay(strMaghribTime));
        calMaghribTime.set(Calendar.MINUTE, Integer.parseInt(strMaghribTime.substring(3,5)));
        calMaghribTime.set(Calendar.SECOND, 0);

        Intent myIntent = new Intent(MainActivity.this, NotificationCreatorReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, 0);

        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC, calMaghribTime.getTimeInMillis(), pendingIntent);

        Toast.makeText(this, "NotificationCreator onReceive()", Toast.LENGTH_SHORT).show();
    } //end of getLocationTime()

这是我的接收器:

public class NotificationCreatorReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Intent service1 = new Intent(context, NotificationCreatorService.class);
        context.startService(service1);

    }
}

这是我的服务:

public class NotificationCreatorService extends Service {


    @Override
    public IBinder onBind(Intent arg0)
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
        // TODO Auto-generated method stub
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Toast.makeText(this, "NotificationCreator onStartCommand()", Toast.LENGTH_SHORT).show();

        // Use NotificationCompat.Builder to set up our notification.
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

        //icon appears in device notification bar and right hand corner of notification
        builder.setSmallIcon(R.mipmap.ic_launcher);

        // This intent is fired when notification is clicked
        Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);
        PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this.getApplicationContext(),
                0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);

        // Set the intent that will fire when the user taps the notification.
        builder.setContentIntent(pendingNotificationIntent);

        // Content title, which appears in large type at the top of the notification
        builder.setContentTitle("It's time for Maghrib");

        // Content text, which appears in smaller text below the title
        builder.setContentText("Maghrib prayer time has started in your area");

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        // Will display the notification in the notification bar
        notificationManager.notify(0, builder.build());

        return START_STICKY;
    }

    @Override
    public void onDestroy()
    {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

}

简短的回答 - 可能是 1 次服务,5 次接收。 Receiver 旨在侦听事件并采取快速行动。如有必要,应使用 Service 来完成所有 "heavy lifting"。

A Receiver 应该侦听一个事件,如果您只需要 post 一个通知,那么您可能只需要这样做就可以了。但是,如果您想做很多其他事情,它应该将 Intent 传递给 Service,并附上数据告诉 Service 如何响应。

编辑:

Receivers 有 10 秒的时间来完成他们的工作,否则将发生 ANR(应用程序无响应)错误。 (参见文档:http://developer.android.com/reference/android/content/BroadcastReceiver.html)创建和发送通知不应花费这么长时间。

但是,"good design" 意味着您应该获取唤醒锁,然后将意图传递给 Service 以执行大部分操作。此外,您可能会发现在某些时候您会想要执行 "other processing"。但是,如果我是你,并且只需要 post 通知,我就会使用 Receiver 并稍后再担心。我可能已经以这种方式无误地处理了超过十亿条通知。但是代码审查者可能会争辩说 "possible" 会发生 ANR...废话...废话...废话...