면책 조항을 볼 수 있습니다.
프로덕션 응용 프로그램에서 아래 코드를 사용했으며 작동합니다. 그러나 나는 당신에게 아주 좋은 시작을 제공해야하는 기본 샘플로 그것을 (특정 애플 리케이션 참조 및 코드를 제거) 편집했습니다.
정적 변수 mIsAppVisible
변수는 앱에서 어디에서나 호출 할 수 있으므로 (앱에서 App
클래스를 통해) 앱이 포커스/가시성이 있어야하는 조건에 따라 코드가 실행되어야하는지 확인합니다. 또한,
public class App extends Application {
public static boolean mIsAppVisible = false;
...
}
는 "부모"활동을 만들기 클래스 등의 응용 프로그램이 실제로 상호 작용 있는지 ParentActivity
을 확장하여 활동에 mIsAppInBackground
을 확인하는 수
모든 다른 활동들도 확장됩니다.
public class ParentActivity extends Activity {
public static boolean mIsBackPressed = false;
public static boolean mIsAppInBackground = false;
private static boolean mIsWindowFocused = false;
public boolean mFailed = false;
private boolean mWasScreenOn = true;
@Override
protected void onStart() {
applicationWillEnterForeground();
super.onStart();
}
@Override
protected void onStop() {
super.onStop();
applicationDidEnterBackground();
}
@Override
public void finish() {
super.finish();
// If something calls "finish()" it needs to behave similarly to
// pressing the back button to "close" an activity.
mIsBackPressed = true;
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
mIsWindowFocused = hasFocus;
if (mIsBackPressed && !hasFocus) {
mIsBackPressed = false;
mIsWindowFocused = true;
}
if (!mIsWindowFocused && mFailed)
applicationDidEnterBackground();
if (isScreenOn() && App.mIsAppVisible && hasFocus) {
// App is back in focus. Do something here...
// this can occur when the notification shade is
// pulled down and hidden again, for example.
}
super.onWindowFocusChanged(hasFocus);
}
@Override
public void onResume() {
super.onResume();
if (!mWasScreenOn && mIsWindowFocused)
onWindowFocusChanged(true);
}
@Override
public void onBackPressed() {
// this is for any "sub" activities that you might have
if (!(this instanceof MainActivity))
mIsBackPressed = true;
if (isTaskRoot()) {
// If we are "closing" the app
App.mIsAppVisible = false;
super.onBackPressed();
} else
super.onBackPressed();
}
private void applicationWillEnterForeground() {
if (mIsAppInBackground) {
mIsAppInBackground = false;
App.mIsAppVisible = true;
// App is back in foreground. Do something here...
// this happens when the app was backgrounded and is
// now returning
} else
mFailed = false;
}
private void applicationDidEnterBackground() {
if (!mIsWindowFocused || !isScreenOn()) {
mIsAppInBackground = true;
App.mIsAppVisible = false;
mFailed = false;
// App is not in focus. Do something here...
} else if (!mFailed)
mFailed = true;
}
private boolean isScreenOn() {
boolean screenState = false;
try {
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
screenState = powerManager.isInteractive();
} catch (Exception e) {
Log.e(TAG, "isScreenOn", e);
}
mWasScreenOn = screenState;
return screenState;
}
}
당신이 당신의 활동에 방법을 만들 수 있습니다 귀하의 사용 (코드는
MainActivity
가정) 즉
penguin이 제안
t.cancel();
메소드를 호출 할 수있는 애니메이션을 처리에 대한
.
if (this instanceof MainActivity) {
((MainActivity) this).cancelTimer();
}
을 아니면 ParentActivity
클래스에 타이머를 추가 한 다음 instanceof
검사 또는 별도의 방법이 필요 없습니다 : 그런 다음 ParentActivity.applicationDidEnterBackground()
방법에 다음을 추가 할 수 있습니다.