最佳答案
Android Studio:
Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run)
So 2 questions:
#1 How do you call a startService
from a static method without a static variable for context?
#2 How do you send a localBroadcast from a static method (same)?
Examples:
public static void log(int iLogLevel, String sRequest, String sData) {
if(iLogLevel > 0) {
Intent intent = new Intent(mContext, LogService.class);
intent.putExtra("UPDATE_MAIN_ACTIVITY_VIEW", "UPDATE_MAIN_ACTIVITY_VIEW");
mContext.startService(intent);
}
}
or
Intent intent = new Intent(MAIN_ACTIVITY_RECEIVER_INTENT);
intent.putExtra(MAIN_ACTIVITY_REQUEST_FOR_UPDATE, sRequest));
intent.putExtra(MAIN_ACTIVITY_DATA_FOR_VIEW, sData);
intent.putExtra(MAIN_ACTIVITY_LOG_LEVEL, iLogLevel);
LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);
What would be the correct way to do this without using mContext
?
NOTE: I think my main question might be how to pass context to a class from which the calling method lives.