Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class MyIntentService extends IntentService {
private static final String TAG = MyIntentService.class.getSimpleName();
private ArrayList<Object> objects;
private long timeMillisInBackground;
public MyIntentService() {
super("MyIntentService");
}
@Override protected void onHandleIntent(Intent intent) {
objects = new ArrayList<>();
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
final PowerManager.WakeLock wakeLock =
powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakelockTag");
wakeLock.acquire();
for (int i = 0; true; i++) {
Log.d(TAG,
String.format("run: Working %d, Time In Background: %s s ,WakeLock is held? : %s", i,
Math.ceil(System.currentTimeMillis() - timeMillisInBackground) / 1000,
wakeLock.isHeld()));
try {
Thread.sleep(1000L);
for (int j = 0; j < 10000; j++) {
//just do some heavy staff to cause GC to work hardly
objects.add(new Rect(10, 10, 10, 10));
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override public void onLowMemory() {
Log.d(TAG, "onLowMemory: ");
}
@Override public void onTrimMemory(int level) {
if (level == 20) {
timeMillisInBackground = System.currentTimeMillis();
}
Log.d(TAG, "onTrimMemory: " + level);
}
@Override public void onDestroy() {
Log.d(TAG, "onDestroy: ");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment