0
我想循環裏面的處理程序每2秒來模擬主頁按鈕按下使用sharedpreference標誌isHomeActive。它會嘗試在服務內部每隔2秒檢查一次該標誌是否處於活動狀態。如果該值爲「否」,它將嘗試重新啓動應用程序,但如果是,應用程序將不會重新啓動。我無法獲得循環工作至今試圖使用線程來循環服務內部來處理主頁按鈕
的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();
}
}
}
};
你如何開始你的runnable? – 2012-08-07 17:06:53