'isGooglePlayServicesAvailable'(this) is deprecated - java

I'm following a tutuorial to use google location services. So, I'm trying to get location using Google API service. But I got isGooglePlayServicesAvailable(this) is deprecated error. Can anyone help. Thanks
Below is my code for checking if Play services exist on device:
private boolean checkPlayServices(){
int resultCode = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(this);
if(resultCode != ConnectionResult.SUCCESS){
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)){
GooglePlayServicesUtil.getErrorDialog(resultCode, this,
PLAY_SERVICES_RESOLUTION_REQUEST).show();
}else {
Toast.makeText(getApplicationContext(),
"This device is not supported", Toast.LENGTH_LONG)
.show();
finish();
}
return false;
}
return true;
}

Use isGooglePlayServicesAvailable instead
GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context)

The correct way of checking if play services are available is:
private boolean isGooglePlayServicesAvailable(Context context) {
GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
int resultCode = googleApiAvailability.isGooglePlayServicesAvailable(context);
return resultCode == ConnectionResult.SUCCESS;
}

Related

Why can't I use my newly created API key?

I had just now created a new API key after enabling Places API in the developer console.
Code for implementing Place picker API:
Places.initialize(getApplicationContext(), "#string/google_place_api");
List<com.google.android.libraries.places.api.model.Place.Field> placeFields = new ArrayList<>(Arrays.asList(com.google.android.libraries.places.api.model.Place.Field.values()));
List<TypeFilter> typeFilters = new ArrayList<>(Arrays.asList(TypeFilter.values()));
// Create a RectangularBounds object.
RectangularBounds bounds = RectangularBounds.newInstance(
new LatLng(-33.880490, 151.184363),
new LatLng(-33.858754, 151.229596));
Intent autocompleteIntent =
new Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, placeFields)
.setLocationBias(bounds)
.setTypeFilter(typeFilters.get(0))
.build(getApplicationContext());
startActivityForResult(autocompleteIntent, 1001);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1001) {
if (resultCode == RESULT_OK) {
Place place = Autocomplete.getPlaceFromIntent(data);
Toast.makeText(getApplicationContext(),place.getName()+ ", " + place.getId(), Toast.LENGTH_SHORT).show();
} else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
// TODO: Handle the error.
Status status = Autocomplete.getStatusFromIntent(data);
Toast.makeText(getApplicationContext(), status.getStatusMessage(), Toast.LENGTH_SHORT).show();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), "Please select a location", Toast.LENGTH_SHORT).show();
}
}
}
The problem is that I keep getting a toast showing The provided API key is invalid whenever I try to search for a location. How is this possible? The API key I got is completely new.
Edit: This is the error that I keep getting in my Logcat
The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
2019-04-29 19:36:10.285 14975-14975/com.example.myapplication E/Places: Error while autocompleting: REQUEST_DENIED
You are incorrectly passing the API Key.
Replace this line:
Places.initialize(getApplicationContext(), "#string/google_place_api");
with:
Places.initialize(getApplicationContext(), getString(R.string.google_place_api));

Programmatically check if a Google App can access your location (not GPS)

Google Apps can access your location, but this functionality may be either enabled or disabled:
How can I programmatically check if a Google App can access your location?
I am use this code but nothing is changing.
boolean servicesOK() {
int isAvailable = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(this);
if (isAvailable == ConnectionResult.SUCCESS) {
return true;
} else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable,
this, AtlastActivity.GPS_ERRORDIALOG_REQUEST);
dialog.show();
} else {
}

ZXing SCAN_RESULT if statements

this is my first question on stack*overflow*!
I am using Eclipse with Android Development Tools. I have copied the code from GitHub - ZXing to scan a QR code using Barcode Scanner (many of you have seen this code before, I'm sure):
public void onClick(View v)
{
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent,0);
}
I am able to get the Barcode Scanner started and it returns results in the onActivityResult() method.
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
if (requestCode == 0)
{
if (resultCode == RESULT_OK)
{
contents = intent.getStringExtra("SCAN_RESULT");
format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan
if(contents == "1")
{
contentsLat = contents;
}
else if(contents == "0")
{
contentsRow = contents;
}
else
{
Toast.makeText(getApplicationContext(), "Code Not Recognized\n"+contents+"\n1", Toast.LENGTH_LONG).show();
}
}
else if (resultCode == RESULT_CANCELED)
{
// Handle cancel
Toast.makeText(getApplicationContext(), "Unsuccessful Scan", Toast.LENGTH_SHORT).show();
}
}
}
However, the else statement is always executed...unless I explicitly equate contents to 1. Below is a link to the screenshot (I don't have embedded image privileges yet) of the else body being executed and the toast clearly indicates that contents=1. What have I done wrong? Did I miss something?
Thanks everybody.
Welcome to stackoverflow!
I have seen this is a very common issue in new Java programmmers :), for String comparison we must use the equals() method.
Change your code to:
// Handle successful scan
if(contents.equals("1")){
contentsLat = contents;
}
else if(contents.equals("0")){
contentsRow = contents;
}
More info:
Java comparison with == of two strings is false?
The equals() Method

isGooglePlayServicesAvailable returning 9

I have the following code in my app to initiate Map View
private boolean servicesOk() {
int isAvailable = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(this);
if (isAvailable == ConnectionResult.SUCCESS) {
return true;
} else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable,
this, GPS_ERRORDIALOG_REQUEST);
dialog.show();
} else {
Toast.makeText(this, "Connect Connect to Maps", Toast.LENGTH_SHORT)
.show();
}
return false;
}
But on the check the isAvailable has the value 9, moreover in LogCat its continously repeating message: Google Play services is invalid. Cannot recover.
Can anyone please tell me, what mistake Am I making?
From the documentation 9 is an invalid/inauthentic install of google play services.
Have you made sure you downloaded and updated gps from the play store?

How do i discover My Bluetooth Device and add the list to my Listview in android?

I am writing a android program to pair and communicate with the BLUETOOTH hardware. I am able to switch ON and OFF the Bluetooth device... But i am not able to search the Bluetooth devices on range and store or display(I am new to android and java).
I could not understand the code in developer.android.com .
So i am posting here... Please help.. Thank u.
HERE IS MY CODE..
public void onToggleClicked(View view) {
if (!((Switch) findViewById(R.id.switch1)).isChecked()) {
mBluetoothAdapter.disable();
} else {
Intent enableBtIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == REQUEST_ENABLE_BT) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
Toast.makeText(this.getBaseContext(),
"Bluetooth IS switched ON now...", Toast.LENGTH_SHORT)
.show();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this.getBaseContext(),
"Bluetooth IS switched OFF now...", Toast.LENGTH_SHORT)
.show();
((Switch) findViewById(R.id.switch1)).setChecked(false);
}
}
}

Categories

Resources