gpt4 book ai didi

java - onCreate 被调用两次导致应用程序过早退出

转载 作者:行者123 更新时间:2023-12-04 07:44:49 26 4
gpt4 key购买 nike

我浏览了其他 SO 帖子,但没有找到与我的问题相关的任何内容。

我正在尝试创建一个应用程序,该应用程序将获取用户上次重新启动后的步数。这是我的主要 Activity :

package com.assignment.bwowassignment;

import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;

import java.util.Arrays;

public class MainActivity extends AppCompatActivity implements MainActivityContract.View {

MainActivityPresenter presenter;

@RequiresApi(api = Build.VERSION_CODES.Q)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

Log.d("mihir", "hello");
presenter = new MainActivityPresenter(this);
presenter.requestActivityPermission(this);
}

@Override
public void onGetStepsSuccess(float[] values) {
Log.d("mihir", Arrays.toString(values));
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == presenter.ACTIVITY_REQUEST_CODE)
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
presenter.registerListener(this);
else {
Log.d("mihir", "kill me");
finish();
}
}

@Override
protected void onPause() {
super.onPause();
presenter.sensorManager.unregisterListener(presenter);
}
}

MainActivityPresenter.java

package com.assignment.bwowassignment;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Build;
import android.util.Log;

import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.logging.Logger;

public class MainActivityPresenter implements MainActivityContract.Presenter, SensorEventListener {

MainActivityContract.View view;
SensorManager sensorManager;
Sensor sensor;

public MainActivityPresenter(MainActivityContract.View view) {
this.view = view;
sensorManager = (SensorManager) ((Context) view).getSystemService(Context.SENSOR_SERVICE);
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
}

@Override
public void registerListener(Context context) {
sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}

@RequiresApi(api = Build.VERSION_CODES.Q)
@Override
public void requestActivityPermission(Context context) {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACTIVITY_RECOGNITION) == PackageManager.PERMISSION_GRANTED)
registerListener(context);
else if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.ACTIVITY_RECOGNITION))
new AlertDialog.Builder(context)
.setTitle("Permission required")
.setMessage("This permission is required to fetch the sensor data from your phone, denying it will cause the app to exit")
.setPositiveButton("Give permission", (dialog, which) -> ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.ACTIVITY_RECOGNITION}, ACTIVITY_REQUEST_CODE))
.setNegativeButton("Deny", (dialog, which) -> ((Activity) context).finish())
.show();
else
ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.ACTIVITY_RECOGNITION}, ACTIVITY_REQUEST_CODE);
}

@Override
public void onSensorChanged(SensorEvent event) {
view.onGetStepsSuccess(event.values);
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}
}

MainActivityContract.java

package com.assignment.bwowassignment;

import android.content.Context;

public interface MainActivityContract {

interface View {
void onGetStepsSuccess(float[] values);
}

interface Presenter {
int ACTIVITY_REQUEST_CODE = 1;

void registerListener(Context context);
void requestActivityPermission(Context context);
}
}

正如您所看到的,标签中有两个带有我名字的日志,这里发生的情况是“hello”日志被记录了两次。我猜这是第一个 onCreate,它负责我的应用程序在启动之前完成,因为我在 logcat 中收到此警告:

W/Activity:一次只能请求一组权限

我只请求一个权限,但由于 onCreate 被调用两次,第一个权限被自动拒绝,这导致应用程序在启动之前finish()!从昨天开始我一直在摸索这个问题,非常感谢任何帮助!

最佳答案

我明白了!

罪魁祸首是这一行:AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

很明显,这会导致配置更改,导致 onCreate 方法被调用两次,要克服这个问题,请在 onCreate 中调用 super 之前调用它,如下所示:

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

关于java - onCreate 被调用两次导致应用程序过早退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67249139/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com