ハンドラー内で 2 秒ごとにループして、sharedpreference フラグ isHomeActive を使用してホーム ボタンの押下をシミュレートしようとしています。サービス内でフラグがアクティブかどうかを 2 秒ごとにチェックしようとします。値が no の場合、アプリケーションを再起動しようとしますが、yes の場合、アプリケーションは再起動されません。これまでループを機能させることができません
メイン アクティビティの onResume および onPause:
@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();
}
サービス クラス内のループ:
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();
}
}
}
};