如何在ViewModel中获取CameraManager而不保留对我的活动/上下文的引用?

时间:2019-04-30 15:23:40

标签: android android-activity android-preferences android-camera2 android-viewmodel

在我的应用程序中,我正在使用Android Jetpack以及导航架构组件。所以现在我想添加多个设置片段的设置片段。到目前为止,导航工作正常,没有问题-现在,我得到了一个摄像头设置片段,用户可以在其中进行一些有关摄像头的设置。问题是我现在想用设备相机提供的可能的分辨率填充ListPreference。因此,我所有的设置片段都将使用我的SettingsViewModel来保存数据。我以为可以在SettingsViewModel中创建一个方法来获得这些分辨率,但是我不知道如何在不违反对我的活动的引用的情况下获得CameraManager

MainActivity.java

    public class MainActivity extends AppCompatActivity implements
        NavigationView.OnNavigationItemSelectedListener,
        ActivityCompat.OnRequestPermissionsResultCallback,
        PreferenceFragmentCompat.OnPreferenceStartFragmentCallback{

...

    @Override
    public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller, Preference pref) {

        switch (pref.getTitle().toString()){
            case "Camera":
                Log.i(TAG, "camera settings selected");

                navController.navigate(R.id.cameraSettingFragment);

                }
        return true;
    }

CameraSettingFragment.java

    public class CameraSettingFragment extends PreferenceFragmentCompat {

    private static final String TAG = "CameraSettingFragment";

    private SettingsViewModel mViewModel;

    @Override
    public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
        setPreferencesFromResource(R.xml.camera_preferences, rootKey);

        mViewModel = ViewModelProviders.of(this).get(SettingsViewModel.class);

        setupCameraPreferences();

    }

    private void setupCameraPreferences() {

        getPossibleFrameRateRanges();

        getPossibleCameraResolutions();

    }



    private void getPossibleFrameRateRanges(){

        final ListPreference listPreference = findPreference("framerate");

        CharSequence[] entries = listPreference.getEntries();

        if(entries == null){

            mViewModel.getPossibleFrameRateRanges();

        }

我想在ViewModel上做一些关于相机特性的事情

    import static android.content.Context.CAMERA_SERVICE;

    public class SettingsViewModel extends AndroidViewModel {

    public SettingsViewModel(@NonNull Application application) {
        super(application);
    }

    public void doAction() {
        // depending on the action, do necessary business logic calls
    }

    public void getPossibleFrameRateRanges() {
        // THIS LINE IS BUGGY AND NEEDS A FIX
        CameraManager manager = (CameraManager) getSystemService(CAMERA_SERVICE);
    }
}

所以我如何不违反这些行:

  

警告:ViewModel绝不能引用视图,生命周期或任何其他视图   可能包含对活动上下文的引用的类。

还是我错过了什么?预先感谢!

1 个答案:

答案 0 :(得分:0)

很抱歉浪费您的时间!我没有读足够的东西……我可以像我确实用AndroidViewModel扩展我的SettingsViewModel类并使用application

    public class SettingsViewModel extends AndroidViewModel {

    private Application application;

    public SettingsViewModel(@NonNull Application application) {
        super(application);
        this.application = application;
    }

    public void doAction() {
        // depending on the action, do necessary business logic calls
    }

    public void getPossibleFrameRateRanges() {

        CameraManager manager = (CameraManager) application.getSystemService(CAMERA_SERVICE);
    }
}

对不起,谢谢!