Android 我应该为我的应用程序创建5个不同的接收器/服务吗?

Android 我应该为我的应用程序创建5个不同的接收器/服务吗?,android,service,broadcastreceiver,alarmmanager,Android,Service,Broadcastreceiver,Alarmmanager,我刚开始在学校和工作之外申请,不知道该走哪条路。基本上,我正在构建一个应用程序,计算每天变化的5次不同的祈祷时间(5次祈祷分别命名为Fajr、Zuhr、Asr、Maghrib和Isha)。计算是在设备上本地完成的,我找到了这方面的开源代码,并让它正确地计算它们。一旦调用了getTimes()方法,就应该计算当天的祈祷时间,然后每天重新计算一次。我认为AlarmManager类的setRepeating()方法会很好。我该怎么做呢?计算祈祷时间后,应启动服务,在该确切时间创建通知,通知用户该祈祷了

我刚开始在学校和工作之外申请,不知道该走哪条路。基本上,我正在构建一个应用程序,计算每天变化的5次不同的祈祷时间(5次祈祷分别命名为Fajr、Zuhr、Asr、Maghrib和Isha)。计算是在设备上本地完成的,我找到了这方面的开源代码,并让它正确地计算它们。一旦调用了
getTimes()
方法,就应该计算当天的祈祷时间,然后每天重新计算一次。我认为
AlarmManager
类的
setRepeating()
方法会很好。我该怎么做呢?计算祈祷时间后,应启动服务,在该确切时间创建通知,通知用户该祈祷了。这里的困境是,我不认为我应该使用5种不同的服务/接收者来通知5种不同的祈祷。最好的办法是什么

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

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

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

这是我的听筒:

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
s有10秒的时间完成其工作,否则将发生ANR(应用程序无响应)错误。(请参阅文档:)创建和发送通知不会花费这么长时间


然而,“良好的设计”意味着您应该获得唤醒锁,然后将意图传递给
服务
,以完成大部分工作。此外,您可能会发现,在某个时候,您可能希望进行“其他处理”。但是,如果我是你,只需要发布一个通知,我只需要使用
接收器
,然后再担心它。我可能已经以这种方式处理了超过10亿个通知,没有错误。但是代码评审员可能会争辩说,ANR是“可能”发生的。。。废话。。。废话。。。废话…

哦,既然我只需要发送一个通知,我就可以有5个不同的接收器,而不需要服务?然后,每个接收者将发送一个自定义通知。这会影响应用程序的平滑度吗?谢谢,非常感谢。你认为你能帮我回答另一个问题吗。
setRepeating()
我每天早上12:01会使用服务来重新计算祈祷时间吗?你应该问一个单独的问题-我不熟悉容错能力或其他可能对该功能很重要的问题。在另一个问题中,您可以针对您的需求更具体。(如果你愿意,在这里发布一个链接-我熟悉重复报警,但是你可以通过发布另一个来获得更好的答案。)此外,如果这回答了第一个问题,请指出。我已经表明你已经回答了我的问题。不过我还有一个小问题。我是要使用同一个alarmmanager,还是应该在每次祈祷时实例化并使用另一个alarmmanager?谢谢!对于
AlarmManager
这是一项服务,您可以获得对它的引用,因此您不需要“实例化”它,只需获得对它的引用即可。但是,您将计划单独的报警事件。
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();
    }

}