Android LocationClient class is deprecated but used in documentation
Again Google has released a new API but they haven't updated the documentation :$ After spend some time trying to figure out how it works I got it, here you have a full example using the new/latest Location Service API... Enjoy developing :)
import android.location.Location;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
public class MainActivity extends Activity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
private final String TAG = "MyAwesomeApp";
private TextView mLocationView;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLocationView = new TextView(this);
setContentView(mLocationView);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
@Override
protected void onStart() {
super.onStart();
// Connect the client.
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
// Disconnecting the client invalidates it.
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
public void onConnected(Bundle bundle) {
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(1000); // Update location every second
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
Log.i(TAG, "GoogleApiClient connection has been suspend");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i(TAG, "GoogleApiClient connection has failed");
}
@Override
public void onLocationChanged(Location location) {
mLocationView.setText("Location received: " + location.toString());
}
}
and do not forget to add this permissions to your AndroidManifest.xml file:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Note: if you just need to get the last location (without updates), you can use LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient)
from OnConnected
LocationClient cannot be resolved to a type
Sounds like you may not have added the Google Play Services SDK to your project.
See the documentation at http://developer.android.com/google/play-services/setup.html
LocationClient vs LocationManager
Location Manager
was introduced in Android SDK and can be used as a feature of android.
Location Client
is something that's part of Google Play SDK and is introduced in the recent Google IO 2013.
One can understand that since Location Client is the latest, it is more efficient in getting the location with minimal energy(battery drain) with greater accuracy.
UPDATE: LocationClient is deprecated. You have to use GoogleApiClient. An example of it can be found here.
Google Play Services Team has cleaned up their code and moved LocationClient functionality into GoogleApiClient.
Tutorial for the same is available in
http://developer.android.com/training/location/retrieve-current.html
On following link you can find IO talk about this subject
http://www.youtube.com/watch?v=Bte_GHuxUGc
UPDATE AGAIN
GoogleApiClient has been deprecated again, you have to use GoogleApi based APIs instead.
Can I use deprecated classes?
From the documentation:
A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists. Compilers warn when a deprecated program element is used or overridden in non-deprecated code.
So you can use some deprecated methods but it won't be the best practice because there are better alternative exists(but in some cases this can even be dangerous)
I get a deprecated warning
private PrefManager prefManager;
PrefManager class has been deprecated. Android Documentation
If you did not get notified by android studio then just go check the android docs for the classes you used.
This class was deprecated in API level R. - what does it mean?
R is the code name for upcoming Android 11.
But even if the class gets deprecated, it will probably stay araound for a couple of years. There will just not be any more changes/improvements for it.
Also there's a lot of other, simpler ways to run code in the background nowadays.
Non-deprecated PackagingOptions?
According to source code comments:
@Deprecated(
"This property is deprecated. Use resources.merges instead.",
replaceWith = ReplaceWith("resources.merges")
)
val merges: MutableSet<String>
@Deprecated(
"This method is deprecated. Use resources.merges.add() instead.",
replaceWith = ReplaceWith("resources.merges.add(pattern)")
)
fun merge(pattern: String)
Thus, it becomes:
// In build.gradle.kts
android {
packagingOptions {
resources {
merges.add("META-INF/NOTICE.md")
}
}
}
Related Topics
Google Sign in Failed Com.Google.Android.Gms.Common.Api.Apiexception: 10:
Error Opening Trace File: No Such File or Directory (2)
Android Studio: Gradle - Build Fails -- Execution Failed for Task ':Dexdebug'
What Is the Size Limit for Logcat and How to Change Its Capacity
Replace Selector Images Programmatically
How to Listen for Changes in Contact Database
Android Studio - Local Path Doesn't Exist
How to Set Image Button Backgroundimage for Different State
How to Bring an Activity to Foreground (Top of Stack)
Programmatically Select Item Listview in Android
Converting JSONarray to Arraylist
How to Add a Textview to a Linearlayout Dynamically in Android
How to Pass Arraylist of Objects from One to Another Activity Using Intent in Android
How to Detect User Pressing Home Key in My Activity
Alertdialog.Builder with Custom Layout and Edittext; Cannot Access View