I’m trying to loop inside a handler to every 2 seconds to simulate the home button press using a sharedpreference flag isHomeActive. It will try to check for the flag every 2 seconds inside the service whether it is active. If the value is no, it will try to relaunch the application but if it is yes, the application won’t be relaunched. I am unable to get the loop working so far
onResume and onPause for main activity:
@Override
public void onPause()
{
super.onPause();
SharedPreferences home = PreferenceManager.getDefaultSharedPreferences(PhysicalTheftDialog.this);
Editor edit=home.edit();
edit.putString("isHomeActive", "no");
edit.commit();
}
@Override
public void onResume()
{
super.onResume();
SharedPreferences home = PreferenceManager.getDefaultSharedPreferences(PhysicalTheftDialog.this);
Editor edit=home.edit();
edit.putString("isHomeActive", "yes");
edit.commit();
}
The loop inside service class:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(PhysicalTheftService.this);
final String isHomeActive = sp.getString("isHomeActive", "");
Runnable myRunnable = new Runnable() {
public void run() {
while (isHomeActive.equals("no")) {
try {
Intent physicaldialog = new Intent(PhysicalTheftService.this, PhysicalTheftDialog.class);
physicaldialog.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PhysicalTheftService.this.startActivity(physicaldialog);
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
You are only ever populating
isHomeActiveone time, so the value will never change. You need to populate it inside the loop.