我在泄漏上下文吗?
Am I leaking Context?
我在我的应用程序中使用多个活动中的 AlarmManager
设置闹钟。
为了避免冗余代码,我创建了以下 class:
public class CustomAlarmManager {
private static final String SHARED_PREF_REQUEST_CODE = "requestCode";
private static final String KEY_REQUEST_CODE = "kRequestCode";
private CustomAlarmManager() {
}
public static void setNewAlarm(Context context, long timeInMillis) {
Intent intent = new Intent(SomeOtherClass.ALARM_ACTION);
intent.putExtra(SomeOtherClass.KEY_ALARM_TIME, timeInMillis);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
context.getApplicationContext(),
getNewCode(context),
intent,
PendingIntent.FLAG_ONE_SHOT);
AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= 23) {
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
timeInMillis, pendingIntent);
} else if (Build.VERSION.SDK_INT >= 19) {
am.setExact(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent);
} else {
am.set(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent);
}
}
// this method is for generating unique int values
// for the requestCode param of PendingIntent.getBroadcast()
private static int getNewCode(Context context) {
SharedPreferences prefs = context.getSharedPreferences(
SHARED_PREF_REQUEST_CODE, MODE_PRIVATE);
int oldCode = prefs.getInt(KEY_REQUEST_CODE, Integer.MIN_VALUE);
int newCode = ++oldCode;
prefs.edit().putInt(KEY_REQUEST_CODE, newCode).apply();
return newCode;
}
}
所以当我想设置闹钟时,我可以在我的应用程序中的任何地方调用以下命令:
CustomAlarmManager.setNewAlarm(aContext, someTimeInMillis);
我的问题:
我应该担心 Context
在这里泄漏吗?
我没有存储对它的引用,所以我觉得我很好,但我不确定。
这是个好方法吗?
Should I have to worry about leaking the Context here?
绝对不是。
您将 context
传递给该方法并在那里使用它完成您的工作。你甚至不存储它或稍后使用它,这在这种情况下可能是罪恶的根源。
我觉得没有问题。
泄漏问题通常发生在您需要在将来完成某些任务并且您持有对当前可用对象(可能在该任务发生之前被杀死)的引用时。
如果将非静态内部 class 对象作为参数发送以在将来的特定时间使用,也会发生这种情况。由于非静态内部 class 持有对其父亲的引用,这将是一个很大的内存泄漏。
1- 你没有为你未来的任务保留任何关于你的上下文的参考
2- 你没有使用内部 class 并将你的 class 作为一个单独的文件并且方法是静态的。
所以请确保您安然无恙 ;)
我在我的应用程序中使用多个活动中的 AlarmManager
设置闹钟。
为了避免冗余代码,我创建了以下 class:
public class CustomAlarmManager {
private static final String SHARED_PREF_REQUEST_CODE = "requestCode";
private static final String KEY_REQUEST_CODE = "kRequestCode";
private CustomAlarmManager() {
}
public static void setNewAlarm(Context context, long timeInMillis) {
Intent intent = new Intent(SomeOtherClass.ALARM_ACTION);
intent.putExtra(SomeOtherClass.KEY_ALARM_TIME, timeInMillis);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
context.getApplicationContext(),
getNewCode(context),
intent,
PendingIntent.FLAG_ONE_SHOT);
AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= 23) {
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
timeInMillis, pendingIntent);
} else if (Build.VERSION.SDK_INT >= 19) {
am.setExact(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent);
} else {
am.set(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent);
}
}
// this method is for generating unique int values
// for the requestCode param of PendingIntent.getBroadcast()
private static int getNewCode(Context context) {
SharedPreferences prefs = context.getSharedPreferences(
SHARED_PREF_REQUEST_CODE, MODE_PRIVATE);
int oldCode = prefs.getInt(KEY_REQUEST_CODE, Integer.MIN_VALUE);
int newCode = ++oldCode;
prefs.edit().putInt(KEY_REQUEST_CODE, newCode).apply();
return newCode;
}
}
所以当我想设置闹钟时,我可以在我的应用程序中的任何地方调用以下命令:
CustomAlarmManager.setNewAlarm(aContext, someTimeInMillis);
我的问题:
我应该担心 Context
在这里泄漏吗?
我没有存储对它的引用,所以我觉得我很好,但我不确定。
这是个好方法吗?
Should I have to worry about leaking the Context here?
绝对不是。
您将 context
传递给该方法并在那里使用它完成您的工作。你甚至不存储它或稍后使用它,这在这种情况下可能是罪恶的根源。
我觉得没有问题。
泄漏问题通常发生在您需要在将来完成某些任务并且您持有对当前可用对象(可能在该任务发生之前被杀死)的引用时。
如果将非静态内部 class 对象作为参数发送以在将来的特定时间使用,也会发生这种情况。由于非静态内部 class 持有对其父亲的引用,这将是一个很大的内存泄漏。
1- 你没有为你未来的任务保留任何关于你的上下文的参考
2- 你没有使用内部 class 并将你的 class 作为一个单独的文件并且方法是静态的。
所以请确保您安然无恙 ;)