我可以将一个类(BroadcastReceiver)作为 Android 中的方法参数传递吗?

huangapple go评论79阅读模式
英文:

Can I pass a class (BroadcastReceiver) as a method parameter in Android?

问题

我的Android程序有几十个广播接收器,执行各种不同的任务,因此创建一个通用的接收器来处理所有事情是不切实际的。但是我发现自己有1000多行的代码,一遍又一遍地做着相同的事情,只是在不断地设置指向广播接收器的AlarmManager。

例如:

public void startStatsAlarm() {
    Log.d(TAG, "startStatsAlarm: in start alarm");
    boolean alarmUp = (PendingIntent.getBroadcast(mContext, 1,
        new Intent(mContext, StatsAlarmReceiver.class),
        PendingIntent.FLAG_NO_CREATE) != null);

    if (alarmUp) {
        Log.d(TAG, "startStatsAlarm: alarm already up, skipping");
        return;
    }

    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(System.currentTimeMillis());
    cal.set(Calendar.HOUR_OF_DAY, 23);
    cal.set(Calendar.MINUTE, 30);

    AlarmManager alarmMgr = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(mContext, StatsAlarmReceiver.class);
    PendingIntent statsIntent = PendingIntent.getBroadcast(mContext, 1, intent, 0);
    if (alarmMgr != null) {
        alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, statsIntent);
    }
}

这只是众多类似的代码之一。我需要更改的唯一部分是MINUTE和HOUR、间隔以及广播接收器。肯定有一些方法可以重用相同的代码,而不是一遍又一遍地编写相同的内容。

英文:

My Android program has dozens of broadcast receivers, doing all kinds of different things, such that making a generic receiver to handle everything would be impracticable. But I find myself with > 1000 lines of code, doing the same thing over and over again, just setting AlarmManagers pointing to BroadcastReceivers.

Eg:

    public void startStatsAlarm() {
        Log.d(TAG, "startStatsAlarm: in start alarm");
        boolean alarmUp = (PendingIntent.getBroadcast(mContext, 1,
            new Intent(mContext, StatsAlarmReceiver.class),
            PendingIntent.FLAG_NO_CREATE) != null);

        if(alarmUp){
            Log.d(TAG, "startStatsAlarm: alarm already up, skipping");
            return;
        }

        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(System.currentTimeMillis());
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 30);

        AlarmManager alarmMgr = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(mContext, StatsAlarmReceiver.class);
        PendingIntent statsIntent = PendingIntent.getBroadcast(mContext, 1, intent, 0);
        if (alarmMgr != null) {
            alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, statsIntent);
    }
}

Is one of many. The only things I need to change are the MINUTE and HOUR, Interval, and BroadcastReceiver.
There must be some way of reusing the same code, rather than having the same thing over and over again?

答案1

得分: 1

请尝试这个:

public void startStatsAlarm(Context mContext, BroadcastReceiver mClass, int hour, int minute) {
    Log.d(TAG, "startStatsAlarm: 启动闹钟");
    boolean alarmUp = (PendingIntent.getBroadcast(mContext, 1,
            new Intent(mContext, mClass.getClass()),
            PendingIntent.FLAG_NO_CREATE) != null);
    if (alarmUp) {
        Log.d(TAG, "startStatsAlarm: 闹钟已经存在,跳过");
        return;
    }
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(System.currentTimeMillis());
    cal.set(Calendar.HOUR_OF_DAY, hour);
    cal.set(Calendar.MINUTE, minute);
    AlarmManager alarmMgr = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(mContext, mClass.getClass());
    PendingIntent statsIntent = PendingIntent.getBroadcast(mContext, 1, intent, 0);
    if (alarmMgr != null) {
        alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, statsIntent);
    }
}
英文:

Try this :

public void startStatsAlarm(Context mContext, BroadcastReceiver mClass, int hour, int minute) {
        Log.d(TAG, "startStatsAlarm: in start alarm");
        boolean alarmUp = (PendingIntent.getBroadcast(mContext, 1,
                new Intent(mContext, mClass.getClass()),
                PendingIntent.FLAG_NO_CREATE) != null);
        if (alarmUp) {
            Log.d(TAG, "startStatsAlarm: alarm already up, skipping");
            return;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(System.currentTimeMillis());
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, minute);
        AlarmManager alarmMgr = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(mContext, mClass.getClass());
        PendingIntent statsIntent = PendingIntent.getBroadcast(mContext, 1, intent, 0);
        if (alarmMgr != null) {
            alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, statsIntent);
        }
    }

huangapple
  • 本文由 发表于 2020年9月14日 20:08:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63884007.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定