Android检测自定义Preference类中的close事件

问题描述 投票:0回答:1

我有一个偏好类,其中我实现了一些逻辑。我希望能够检测到该首选项何时关闭,以便我可以断开google API客户端。

我的自定义偏好类。一切正常但是当我关闭首选项并再次打开它时,它会显示一个错误,即具有该ID的API客户端已经存在。

我的首选项类代表整个首选项屏幕,它具有位置自动完成文本编辑器。

的preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
    android:title="Notifications"
    android:key="pref_key_storage_settings">
    <SwitchPreference
        android:key="pref_enable_notifications"
        android:title="News notifications"
        android:defaultValue="true" />
    <EditTextPreference
        android:key="pref_confirms_needed"
        android:digits="0123456789"
        android:inputType="number"
        android:title="@string/pref_confirmsNeeded"
        android:defaultValue="0"
        android:dialogMessage="Enter minimum number of confirms news should have for you to be notified about it."
        android:summary="You will be notified only for news that have higher number of confirms than the number you set here." />
    <Preference
        android:key="pref_location_screen"
        android:persistent="false"
        android:title="Notification range" />
</PreferenceCategory>
</PreferenceScreen>

我没有显示任何代码,因为我想保持简单。如果您需要,请告诉我!

编辑

我添加了代码

LocationPreference.xml

public class LocationPreference extends Preference implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
    private static final String TAG = "LocationPreference";
    private Context mContext;
    private PlaceAutocompleteAdapter mLocationAutocompleteAdapter;
    private GoogleApiClient mGoogleApiClient;
    private static final LatLngBounds LAT_LNG_BOUNDS = new LatLngBounds(
        new LatLng(-40, -168), new LatLng(71, 136));
    private View autocompleteSearch;

    // widgets
    private AutoCompleteTextView mSearchText;

    public LocationPreference(Context context) {
        super(context);
        setLayoutResource(R.layout.location_preference);

        mContext = context;
    }

    public LocationPreference(Context context, AttributeSet attrs)
    {
    super(context, attrs);
        setLayoutResource(R.layout.location_preference);

        mContext = context;
    }

    public LocationPreference(Context context, AttributeSet attrs, int defStyle)     {
        super(context, attrs, defStyle);
        setLayoutResource(R.layout.location_preference);

        mContext = context;
    }


    private void init() {
        if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient
                .Builder(mContext)
                .addApi(Places.GEO_DATA_API)
                .addApi(Places.PLACE_DETECTION_API)
                .enableAutoManage((AppCompatActivity) mContext,this)
                .build();
        }

        mLocationAutocompleteAdapter = new PlaceAutocompleteAdapter(
            mContext, mGoogleApiClient, LAT_LNG_BOUNDS, null);
        mSearchText.setAdapter(mLocationAutocompleteAdapter);

        mSearchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_SEARCH
                    || i == EditorInfo.IME_ACTION_DONE
                    || keyEvent.getAction() == keyEvent.ACTION_DOWN
                        || keyEvent.getAction() == keyEvent.KEYCODE_ENTER) {
                    //geoLocate();
                }

                return false;
            }
        });
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
            mGoogleApiClient.stopAutoManage((AppCompatActivity) mContext);
            mGoogleApiClient.disconnect();
        }
    }

    @Override
    protected View onCreateView(ViewGroup parent) {
        super.onCreateView(parent);
        LayoutInflater li = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (autocompleteSearch == null) {
            autocompleteSearch = li.inflate(R.layout.location_preference, parent, false);
            mSearchText = autocompleteSearch.findViewById(R.id.location_pref_input);
            mSearchText.setOnItemClickListener(mAutocompleteClickListener);
            init();
        }

        return autocompleteSearch;
    }

    private AdapterView.OnItemClickListener mAutocompleteClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            //hideSoftKeyboard();
            Log.d(TAG, "OnItemClick()");
            final AutocompletePrediction item = mLocationAutocompleteAdapter.getItem(i);
            final String placeId = item.getPlaceId();

            PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi
                   .getPlaceById(mGoogleApiClient, placeId);
            placeResult.setResultCallback(mUpdatePlaceDetailsCallback);
        }
    };



    private ResultCallback<PlaceBuffer> mUpdatePlaceDetailsCallback = new ResultCallback<PlaceBuffer>() {
        @Override
        public void onResult(@NonNull PlaceBuffer places) {
            Log.d(TAG, "onResult()");
            if(!places.getStatus().isSuccess()){
                Log.d(TAG, "onResult: Place query did not complete successfully: " + places.getStatus().toString());
                places.release();
                return;
            }
            final Place place = places.get(0);

                persistString(CommonUtils.latLngToStr(place.getViewport().getCenter().latitude,
                place.getViewport().getCenter().longitude));

            SharedPreferences preferences =     PreferenceManager.getDefaultSharedPreferences(mContext);
            Map<String, ?> allEntries = preferences.getAll();
            for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
                Log.d(TAG, entry.getKey() + ": " + entry.getValue().toString());
            }

            places.release();

        }
    };

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {

    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.i(TAG, "Connection suspended");
        mGoogleApiClient.connect();
    }
}

Aaditi

我认为我最好的办法就是检查Google Api客户端是否存在。

android android-preferences
1个回答
0
投票

真的需要查看您的代码并更清楚地了解您要执行的操作。

但是,如果您在用户关闭偏好设置时尝试关闭所有内容,主要是返回到应用主屏幕,您可以尝试断开Goog​​le API客户端的连接。 onBackPressed活动calllback

@Override
public void onBackPressed()
{
    super.onBackPressed();

    // Do your stuff here.
}

实际上,你冷得更深,并在onBackPressed之后触发的Destroy回调中执行

@Override
public void onDestroy()
{
    super.onDestroy();

    // Do your stuff here.
}

在返回您的应用活动之前,这应该是最后一次回调。哦,对不起,对于偏好,请尝试onPrepareForRemoval回调

@Override
public void onPrepareForRemoval()
{
    super.onPrepareForRemoval();

    // Close the google api here.
}

然后,如果这不起作用,请尝试将Google API关闭/断开连接到PreferenceActivity onDestroy回调。

我个人建议重构代码以使用PreferenceFragment而不是Preference。

© www.soinside.com 2019 - 2024. All rights reserved.