计算 android 应用程序进入后台的次数
Counting number of times android app goes into background
我正在开发一个应用程序,它可以计算应用程序进入后台的次数。当方向改变时,它也会保留值。虽然它适用于所有用例,但我有一个用例不起作用。
案例 :当我按下主页按钮时,更改 phone 的方向并重新打开应用程序,它确实以横向模式打开但是,背景计数不增加。
我已经尝试在所有生命周期方法中设置值。它不起作用。希望有人能帮我解决这个问题。
`
public class MainActivity extends AppCompatActivity {
private int clickCount =0, backgroundCount = 0;
TextView tvClickCountValue, tvBackgroundCountValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if( savedInstanceState != null){
clickCount = savedInstanceState.getInt("COUNT");
backgroundCount = savedInstanceState.getInt("BGCOUNT");
}
setContentView(R.layout.activity_main);
tvClickCountValue = (TextView) this.findViewById(R.id.tvClickCountValue);
tvBackgroundCountValue = (TextView) this.findViewById(R.id.tvBackgroundCountValue);
setView(MainActivity.this);
}
public void onClick(View v){
clickCount += 1;
tvClickCountValue.setText(Integer.toString(clickCount));
}
public void setView(Context ctx){
tvClickCountValue.setText(Integer.toString(clickCount));
tvBackgroundCountValue.setText(Integer.toString(backgroundCount));
}
@Override
protected void onStop() {
super.onStop();
backgroundCount += 1;
}
@Override
protected void onResume() {
super.onResume();
tvClickCountValue.setText(Integer.toString(clickCount));
tvBackgroundCountValue.setText(Integer.toString(backgroundCount));
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("COUNT", clickCount);
outState.putInt("BGCOUNT", backgroundCount);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
clickCount = savedInstanceState.getInt("COUNT");
backgroundCount = savedInstanceState.getInt("BGCOUNT");
}
}
您需要将计数保留在 SharedPreferences 中。每次重新打开应用程序时,都会从 SharedPreferences 中读取最后一个值。每次隐藏应用程序时递增并保存到 SharedPreferences。
然后你可以用 @kiran code:
public class BaseActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
public static boolean isAppInFg = false;
public static boolean isScrInFg = false;
public static boolean isChangeScrFg = false;
@Override
protected void onStart() {
if (!isAppInFg) {
isAppInFg = true;
isChangeScrFg = false;
onAppStart();
}
else {
isChangeScrFg = true;
}
isScrInFg = true;
super.onStart();
}
@Override
protected void onStop() {
super.onStop();
if (!isScrInFg || !isChangeScrFg) {
isAppInFg = false;
onAppPause();
}
isScrInFg = false;
}
public void onAppStart() {
// app in the foreground
// show the count here.
}
public void onAppPause() {
// app in the background
// start counting here.
}
}
本文包含有用的信息:https://developer.android.com/guide/topics/resources/runtime-changes.html 尤其是有关处理变更的部分。
尝试在 onConfigurationChanged()
上处理它,您已禁用 Activity 方向更改时重新启动。否则,通过本文您可能会发现哪种情况适用于您的场景。
通过阅读问题描述,我假设如果您不旋转设备,应用程序将按预期工作。
我正在开发一个应用程序,它可以计算应用程序进入后台的次数。当方向改变时,它也会保留值。虽然它适用于所有用例,但我有一个用例不起作用。
案例 :当我按下主页按钮时,更改 phone 的方向并重新打开应用程序,它确实以横向模式打开但是,背景计数不增加。 我已经尝试在所有生命周期方法中设置值。它不起作用。希望有人能帮我解决这个问题。 `
public class MainActivity extends AppCompatActivity {
private int clickCount =0, backgroundCount = 0;
TextView tvClickCountValue, tvBackgroundCountValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if( savedInstanceState != null){
clickCount = savedInstanceState.getInt("COUNT");
backgroundCount = savedInstanceState.getInt("BGCOUNT");
}
setContentView(R.layout.activity_main);
tvClickCountValue = (TextView) this.findViewById(R.id.tvClickCountValue);
tvBackgroundCountValue = (TextView) this.findViewById(R.id.tvBackgroundCountValue);
setView(MainActivity.this);
}
public void onClick(View v){
clickCount += 1;
tvClickCountValue.setText(Integer.toString(clickCount));
}
public void setView(Context ctx){
tvClickCountValue.setText(Integer.toString(clickCount));
tvBackgroundCountValue.setText(Integer.toString(backgroundCount));
}
@Override
protected void onStop() {
super.onStop();
backgroundCount += 1;
}
@Override
protected void onResume() {
super.onResume();
tvClickCountValue.setText(Integer.toString(clickCount));
tvBackgroundCountValue.setText(Integer.toString(backgroundCount));
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("COUNT", clickCount);
outState.putInt("BGCOUNT", backgroundCount);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
clickCount = savedInstanceState.getInt("COUNT");
backgroundCount = savedInstanceState.getInt("BGCOUNT");
}
}
您需要将计数保留在 SharedPreferences 中。每次重新打开应用程序时,都会从 SharedPreferences 中读取最后一个值。每次隐藏应用程序时递增并保存到 SharedPreferences。
然后你可以用 @kiran code:
public class BaseActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
public static boolean isAppInFg = false;
public static boolean isScrInFg = false;
public static boolean isChangeScrFg = false;
@Override
protected void onStart() {
if (!isAppInFg) {
isAppInFg = true;
isChangeScrFg = false;
onAppStart();
}
else {
isChangeScrFg = true;
}
isScrInFg = true;
super.onStart();
}
@Override
protected void onStop() {
super.onStop();
if (!isScrInFg || !isChangeScrFg) {
isAppInFg = false;
onAppPause();
}
isScrInFg = false;
}
public void onAppStart() {
// app in the foreground
// show the count here.
}
public void onAppPause() {
// app in the background
// start counting here.
}
}
本文包含有用的信息:https://developer.android.com/guide/topics/resources/runtime-changes.html 尤其是有关处理变更的部分。
尝试在 onConfigurationChanged()
上处理它,您已禁用 Activity 方向更改时重新启动。否则,通过本文您可能会发现哪种情况适用于您的场景。
通过阅读问题描述,我假设如果您不旋转设备,应用程序将按预期工作。