2017-01-10 146 views
-1

在谷歌播放服务10.0.1,为下面的代码,构建时我得到的错误:无法解析符号“GooglePlayServicesClient”

“GooglePlayServicesClient” cannot resolve the symbol

不知道如何解决这个问题?

import android.app.ProgressDialog; 
import android.content.ContentResolver; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.location.Geocoder; 
import android.location.Location; 
import android.location.LocationManager; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.provider.Settings; 
import android.support.v7.app.AlertDialog; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.view.KeyEvent; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.inputmethod.EditorInfo; 
import android.view.inputmethod.InputMethodManager; 
import android.widget.AdapterView; 
import android.widget.AutoCompleteTextView; 
import android.widget.ListView; 
import android.widget.ProgressBar; 
import android.widget.TextView; 
import android.widget.Toast; 
import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.location.LocationClient; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.common.GooglePlayServicesClient; 
import com.google.android.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<android.location.Address> addresses; 
      try { 
       addresses = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1); 

       if (addresses != null && addresses.size() > 0) { 
        final android.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; 
     } 

    } 

} 
+0

错误出在这两个进口和相应的类(1)进口com.google .android.gms.location.LocationClient; (2)import com.google.android.gms.common.GooglePlayServicesClient; ,我编译了“com.google.android.gms:play-services:10.0.1”,但仍无法解决上述导入问题 –

+0

清理代码和问题以便于阅读/理解 – clearlight

回答

1

在本answer提到我猜GooglePlayServiceClient,并已成为过时因此将其删除,并使用GoogleApiClient instead.Also LocationServices.API,并介绍FusedLocationProviderApi

GoogleApiClient的用法:

要使用GoogleApiClient首先要改变的是接口,活动或服务在GooglePlayServiceClient实施

//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 

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(); 
    } 

替换它^与GoogleApiClient的新建设者代码:

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); 

in GoogleApiClient我们使用FusedLocatonApi像:

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

This post显示你最像的方法实现:onConnectedonConnectionFailedonConnectionSuspended

+0

可以用于“LocationClient”吗? –

+0

我已更新答案,查看可能有用的链接。 – anotherGatsby

+0

用什么来代替“LocationClient mLocationClient”?你能帮我么? –