有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法解析符号“GooglePlayServicesClient”

在Google play service 10.0.1中,对于以下代码,在构建它时,我会得到错误:

“GooglePlayServicesClient” cannot resolve the symbol

知道怎么解决吗

import 安卓.app.ProgressDialog;
import 安卓.content.ContentResolver;
import 安卓.content.Context;
import 安卓.content.DialogInterface;
import 安卓.content.Intent;
import 安卓.location.Geocoder;
import 安卓.location.Location;
import 安卓.location.LocationManager;
import 安卓.os.AsyncTask;
import 安卓.os.Bundle;
import 安卓.provider.Settings;
import 安卓.support.v7.app.AlertDialog;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.support.v7.widget.Toolbar;
import 安卓.text.Editable;
import 安卓.text.TextWatcher;
import 安卓.view.KeyEvent;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.view.View;
import 安卓.view.inputmethod.EditorInfo;
import 安卓.view.inputmethod.InputMethodManager;
import 安卓.widget.AdapterView;
import 安卓.widget.AutoCompleteTextView;
import 安卓.widget.ListView;
import 安卓.widget.ProgressBar;
import 安卓.widget.TextView;
import 安卓.widget.Toast;
import com.google.安卓.gms.common.ConnectionResult;
import com.google.安卓.gms.location.LocationClient;
import com.google.安卓.gms.common.api.GoogleApiClient;
import com.google.安卓.gms.common.GooglePlayServicesClient;
import com.google.安卓.gms.location.LocationRequest;
import com.shuan.Project.R;
import com.shuan.Project.Utils.Common;
import com.shuan.Project.asyncTasks.EmployeeSerchResult;
import com.shuan.Project.asyncTasks.GetEmployeeSerach;
import com.shuan.Project.employer.PostViewActivity;
import java.util.List;
import java.util.Locale;

public class EmplyeeSearchActivity extends AppCompatActivity implements 
GooglePlayServicesClient.ConnectionCallbacks,
        GooglePlayServicesClient.OnConnectionFailedListener {

    private Toolbar toolbar;
    private Common mApp;
    private Boolean flag = false;
    private LocationClient mLocationClient;
    private ProgressDialog pDialog;
    private GoogleApiClient mGoogleApiClient;
    private LocationRequest mLocationRequest;
    private Location mLastLocation;
    private ProgressBar progressBar;
    private ListView list;
    private AutoCompleteTextView preferSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mApp = (Common) getApplicationContext();
        if (mApp.getPreference().getString(Common.LEVEL, "").equalsIgnoreCase("1")) {
            setTheme(R.style.Junior);
        } else {
            setTheme(R.style.Senior);
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_emplyee_search);
        toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);

        if (mApp.getPreference().getString(Common.LEVEL, "").equalsIgnoreCase("1")) {
            toolbar.setBackgroundColor(getResources().getColor(R.color.junPrimary));
        } else {
            toolbar.setBackgroundColor(getResources().getColor(R.color.senPrimary));
        }
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        mLocationClient = new LocationClient(this, this, this);
        mLocationClient.connect();

        mLocationRequest = new LocationRequest();

        progressBar = (ProgressBar) findViewById(R.id.progress_bar);
        list = (ListView) findViewById(R.id.ser_res);
        preferSearch = (AutoCompleteTextView) findViewById(R.id.prefered_serach);

        new GetEmployeeSerach(EmplyeeSearchActivity.this, progressBar, preferSearch).execute();

        preferSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(actionId== EditorInfo.IME_ACTION_SEARCH){
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
                    preferSearch.dismissDropDown();
                    progressBar.setVisibility(View.VISIBLE);
                    new EmployeeSerchResult(EmplyeeSearchActivity.this, progressBar, list, preferSearch.getText().toString(),
                            "all").execute();
                }
                return false;
            }
        });

        preferSearch.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
                TextView txt = (TextView) view.findViewById(R.id.display);
                TextView txt1 = (TextView) view.findViewById(R.id.ins_name);
                preferSearch.setText(txt.getText().toString());
                progressBar.setVisibility(View.VISIBLE);
                new EmployeeSerchResult(EmplyeeSearchActivity.this, progressBar, list, txt.getText().toString(),
                        txt1.getText().toString()).execute();
            }
        });

        preferSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                list.setAdapter(null);
            }
        });

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView txt = (TextView) view.findViewById(R.id.jId);
                Intent in = new Intent(getApplicationContext(), PostViewActivity.class);
                in.putExtra("jId", txt.getText().toString());
                in.putExtra("apply", "no");
                startActivity(in);
            }
        });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.emp_search_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.gps) {
            flag = gpsStatus();
            if (flag) {
                new displayCurrentLocation().execute();
            } else {
                showGpsAlert();
            }
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onConnected(Bundle bundle) {

    }

    @Override
    public void onDisconnected() {

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }


    public class displayCurrentLocation extends AsyncTask<String, String, String> {

        String location;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(EmplyeeSearchActivity.this);
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.setMessage("Searching");
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... params) {

            Location currentLocation = mLocationClient.getLastLocation();

            Geocoder geocoder = new Geocoder(EmplyeeSearchActivity.this, Locale.getDefault());
            Location loc = currentLocation;
            List<安卓.location.Address> addresses;
            try {
                addresses = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);

                if (addresses != null && addresses.size() > 0) {
                    final 安卓.location.Address address = addresses.get(0);
                    location = address.getLocality();
                }

            } catch (Exception e) {
            }
            return location;
        }

        @Override
        protected void onPostExecute(final String s) {
            super.onPostExecute(s);
            pDialog.cancel();
            Intent in = new Intent(getApplicationContext(), EmployeeSearchResultActivity.class);
            in.putExtra("loc", s);
            startActivity(in);


        }
    }

    private void showGpsAlert() {
        AlertDialog.Builder build = new AlertDialog.Builder(EmplyeeSearchActivity.this);
        build.setTitle("Alert")
                .setMessage("Turn On your GPS! Find the Jobs & companies")
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent in = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        startActivity(in);
                        dialog.cancel();
                    }
                }).setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
                Toast.makeText(getApplicationContext(), "Can't Find Employer", Toast.LENGTH_SHORT).show();
            }
        }).show();
    }

    private Boolean gpsStatus() {
        ContentResolver contentResolver = getBaseContext().getContentResolver();
        boolean gpsStus = Settings.Secure.isLocationProviderEnabled(contentResolver, LocationManager.GPS_PROVIDER);
        if (gpsStus) {
            return true;
        } else {
            return false;
        }

    }

}

共 (1) 个答案

  1. # 1 楼答案

    正如本{a1}中提到的,我猜{}已经过时了,所以请删除它,改用{a2}。还介绍了^{}FusedLocationProviderApi

    GoogleAppClient的使用:

    要使用GoogleApiClient首先要更改的是接口,即正在实现的活动或服务

     //old code of GooglePlayServiceClient :
     public class LocationMonitoringService extends Service implements   GooglePlayServicesClient.ConnectionCallbacks  
    
    //replace the above ^ code with this: new code for GoogleApiClient
    public class LocationMonitoringService extends Service implements GoogleApiClient.ConnectionCallbacks  
    

    GooglePlayServiceClient中,mLocationClient只是一个常规的类构造函数。 在GoogleApiClient中使用的生成器如下所示:

    //old code of GooglePlayServiceClient :
    LocationClient mLocationClient;
    
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {  
       mLocationClient = new LocationClient(this, this, this);
       mLocationClient.connect();
      }
    

    将其替换为GoogleAppClient的新生成器代码:

    GoogleApiClient mLocationClient;
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {  
        mLocationClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    
        mLocationClient.connect();
    }
    

    示例: 请求地点: 旧代码只是一个简单的调用,比如

    mLocationClient.requestLocationUpdates(mLocationRequest, locationListener);  
    

    GoogleApiClient中,我们使用FusedLocatonApi类似:

    LocationServices.FusedLocationApi.requestLocationUpdates(mLocationClient, mLocationRequest, locationListener); 
    

    This post向您展示了大多数方法的实现,如:onConnected{}{}等