Neither
Permission Denied nor Permission Granted are showing up in my log.
However the Log.v("SPAM","1"); is working.
Does the class have to be the "MainActivity" running in order for the code to work ?
This is the class where the permissions are being used.
Another problem was that requestPermissions was not running, and the code below wasn't either. Any ideas what this could be?
package com.project.backgroundprocesstest;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import org.json.JSONObject;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Locale;
/**
* Created by Alex on 14/09/2016.
*/
public class LocationControl extends AppCompatActivity{
final int REQUEST_ACCESS_FINE_LOCATION = 5;
private Context context;
private LocationManager locationManager;
public LocationControl(Context context){
this.context = context;
this.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if(ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
Log.v("SPAM", "1");
this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new MyLocationListener(context));
}
}
public LocationManager getLocationManager(){
return locationManager;
}
public String getLocation(){
Log.v("SPAM", "1");
if(ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Log.v("CheckPermission","Permission Denied");
ActivityCompat.requestPermissions(this,new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},REQUEST_ACCESS_FINE_LOCATION);
Log.v("CheckPermission","It Does run");
}
else{
Log.v("CheckPermission","Permission Granted");
Location location = this.locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitudinal = location.getLongitude();
double latitudinal = location.getLatitude();
String cityName = findCity(location);
Date date = new Date();
return setJSON(longitudinal,latitudinal,cityName,date);
}
return null;
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,int[] grantResults){
switch(requestCode){
case REQUEST_ACCESS_FINE_LOCATION: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
Log.d("CheckPermission","User Denisd Permission");
}
return;
}
}
}
public String setJSON(double lng, double lat, String place, Date date) {
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject();
jsonObject.put("longitudinal", lng);
jsonObject.put("latitudinal", lat);
jsonObject.put("name", place);
jsonObject.put("created", date);
} catch (Exception e) {
Log.e("Error", "JSON ERROR");
}
if(jsonObject != null)
return jsonObject.toString();
else
return null;
}
private String findCity(Location loc){
String cityName = null;
Geocoder gcd = new Geocoder(context, Locale.getDefault());
// IF any addresses which belong to the Longitudinal and Latitudinal are found,
// set cityName and print it
List<Address> addresses;
try{
addresses = gcd.getFromLocation(loc.getLatitude(),loc.getLongitude(),1 );
if(addresses.size()>0){
System.out.println(addresses.get(0).getLocality());
cityName = addresses.get(0).getLocality();
}
}catch(IOException e){
e.printStackTrace();
}
return cityName;
}
private String locationToString(Location location){
return Location.convert(location.getLatitude(),Location.FORMAT_DEGREES)+" " + Location.convert(location.getLongitude(),Location.FORMAT_DEGREES);
}
}
EDIT: No errors are popping up in log or debug.
You can call checkCallingPermission (permission) of Context instead
Related
In my Android project, sometimes I am able to get the user location and sometimes I get NULL location. I have tried out all possible scenarios but not able to get the reason for the NULL location. This is the Main Activity file.
I have also added ACCESS_FINE_LOCATION and INTERNET permission in the manifest file and implementation 'com.google.android.gms:play-services-location:16.0.0' in build.gradle file.
import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnSuccessListener;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private static final int MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;
Context context = this;
Button fetch;
TextView user_location;
TextView user_address;
Double latitude;
Double longitude;
private FusedLocationProviderClient mFusedLocationClient;
Geocoder geocoder;
List<Address> addresses;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fetch = findViewById(R.id.fetch_location);
user_location = findViewById(R.id.user_location);
user_address = findViewById(R.id.user_address);
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
fetch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
fetchLocation();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private void fetchLocation() throws IOException {
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
new AlertDialog.Builder(this)
.setTitle("Required Location Permission")
.setMessage("You have to give this permission to acess this feature")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
})
.create()
.show();
} else {
// No explanation needed; request the permission
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
} else {
// Permission has already been granted
mFusedLocationClient.getLastLocation()
.addOnSuccessListener(MainActivity.this, new OnSuccessListener<Location>() {
#Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations this can be null.
if (location != null) {
// Logic to handle location object
latitude = location.getLatitude();
longitude = location.getLongitude();
user_location.setText("Latitude = "+latitude + "\nLongitude = " + longitude);
geocoder = new Geocoder(context, Locale.getDefault());
user_address.setText("Latitude = "+latitude + "\nLongitude = " + longitude);
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
} catch (IOException e) {
e.printStackTrace();
}
String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
String knownName = addresses.get(0).getFeatureName(); // Only if available else return NULL
user_address.setText(address+ " \n" + city + " \n" + state + "\n " + country + "\n " + postalCode + "\n " + knownName);
}
else
{
user_location.setText("Latitude, Longitude not able to access");
user_address.setText("Not Done!!!!!!!!!");
}
}
});
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if(requestCode == MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION){
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
//abc
}else{
}
}
}
}
Can anyone please guide me how to send the longitude and latitude of your phone via text message to another phone number? In which format do I enter the phone number, local or International.
Here is my code, permission is already in the manifest:
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.telephony.SmsManager;
import android.widget.EditText;
public class NewActivity extends AppCompatActivity {
static final int REQUEST_LOCATION = 1;
LocationManager locationManager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from new_activity.xml
setContentView(R.layout.new_activity);
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
getLocation();
}
void getLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
} else {
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
double latti = location.getLatitude();
double longi = location.getLongitude();
((EditText) findViewById(R.id.Lati)).setText("Latitude: " + latti);
((EditText) findViewById(R.id.Longi)).setText("Longitude: " + longi);
try {
SmsManager manager = SmsManager.getDefault();
String message;
String telNumber;
telNumber = "XXXXXXXXXX";
// Get the string
message = "[AUTOMATIC MESSAGE]\n" + "I am currently at \n"+"Latitude" + latti +
"and \n"+"Longitude" + longi + " (approximately; accuracy within 100 meters).";
manager.sendTextMessage(telNumber, null, message, null, null);
} catch (Exception ignored) {
}
}else {
((EditText) findViewById(R.id.Lati)).setText("Unable to find correct location.");
((EditText) findViewById(R.id.longi)).setText("Unable to find correct location. ");
}
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions,#NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_LOCATION:
getLocation();
break;
}
}
}
The telephone number must be
tel: phone number
I am developing an application , when use click on image, google map should be open,when map open first it show the current location of user ,after when user click any where on map marker should be placed on submit it should take a street name ,pincode(zipcode) ,state and country and want this data automatially to be filled in editbox.
I am new android so not understanding how to do it
Thanks in advance
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
try {
addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getLocality();
} catch (IOException e) {
e.printStackTrace();
}
Please Note :
location.getLatitude() = your latitude value,
location.getLongitude() = your longitude value.
In order to achieve this, you need to follow some steps.
Step1:
Adding Location Api Lib:
implementation 'com.google.android.gms:play-services-location:11.6.0'
Step2:
Add metadata to AndroidManifest.xml:
replace 'key' with your Api-key
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="key"/>
Step3:
You need a service and one activity to work on:
1.AddressListActivity.java
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.ResultReceiver;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
public class AddressListActivity extends AppCompatActivity {
private FusedLocationProviderClient fusedLocationClient;
private static final int LOCATION_PERMISSION_REQUEST_CODE = 2;
private LocationAddressResultReceiver addressResultReceiver;
private TextView currentAddTv;
private Location currentLocation;
private LocationCallback locationCallback;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.current_location_address_layout);
Toolbar tb = findViewById(R.id.toolbar);
setSupportActionBar(tb);
tb.setSubtitle("Current Location Address");
addressResultReceiver = new LocationAddressResultReceiver(new Handler());
currentAddTv = findViewById(R.id.current_address);
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
locationCallback = new LocationCallback() {
#Override
public void onLocationResult(LocationResult locationResult) {
currentLocation = locationResult.getLocations().get(0);
getAddress();
};
};
startLocationUpdates();
}
#SuppressWarnings("MissingPermission")
private void startLocationUpdates() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
LOCATION_PERMISSION_REQUEST_CODE);
} else {
LocationRequest locationRequest = new LocationRequest();
locationRequest.setInterval(2000);
locationRequest.setFastestInterval(1000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
fusedLocationClient.requestLocationUpdates(locationRequest,
locationCallback,
null);
}
}
#SuppressWarnings("MissingPermission")
private void getAddress() {
if (!Geocoder.isPresent()) {
Toast.makeText(AddressListActivity.this,
"Can't find current address, ",
Toast.LENGTH_SHORT).show();
return;
}
Intent intent = new Intent(this, GetAddressIntentService.class);
intent.putExtra("add_receiver", addressResultReceiver);
intent.putExtra("add_location", currentLocation);
startService(intent);
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions,
#NonNull int[] grantResults) {
switch (requestCode) {
case LOCATION_PERMISSION_REQUEST_CODE: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startLocationUpdates();
} else {
Toast.makeText(this, "Location permission not granted, " +
"restart the app if you want the feature",
Toast.LENGTH_SHORT).show();
}
return;
}
}
}
private class LocationAddressResultReceiver extends ResultReceiver {
LocationAddressResultReceiver(Handler handler) {
super(handler);
}
#Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
if (resultCode == 0) {
//Last Location can be null for various reasons
//for example the api is called first time
//so retry till location is set
//since intent service runs on background thread, it doesn't block main thread
Log.d("Address", "Location null retrying");
getAddress();
}
if (resultCode == 1) {
Toast.makeText(AddressListActivity.this,
"Address not found, " ,
Toast.LENGTH_SHORT).show();
}
String currentAdd = resultData.getString("address_result");
showResults(currentAdd);
}
}
private void showResults(String currentAdd){
currentAddTv.setText(currentAdd);
}
#Override
protected void onResume() {
super.onResume();
startLocationUpdates();
}
#Override
protected void onPause() {
super.onPause();
fusedLocationClient.removeLocationUpdates(locationCallback);
}
}
2.Your Intent Service as GetAddressIntentService
import android.app.IntentService;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.os.ResultReceiver;
import android.support.annotation.Nullable;
import android.util.Log;
import java.util.List;
import java.util.Locale;
public class GetAddressIntentService extends IntentService {
private static final String IDENTIFIER = "GetAddressIntentService";
private ResultReceiver addressResultReceiver;
public GetAddressIntentService() {
super(IDENTIFIER);
}
//handle the address request
#Override
protected void onHandleIntent(#Nullable Intent intent) {
String msg = "";
//get result receiver from intent
addressResultReceiver = intent.getParcelableExtra("add_receiver");
if (addressResultReceiver == null) {
Log.e("GetAddressIntentService",
"No receiver, not processing the request further");
return;
}
Location location = intent.getParcelableExtra("add_location");
//send no location error to results receiver
if (location == null) {
msg = "No location, can't go further without location";
sendResultsToReceiver(0, msg);
return;
}
//call GeoCoder getFromLocation to get address
//returns list of addresses, take first one and send info to result receiver
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(
location.getLatitude(),
location.getLongitude(),
1);
} catch (Exception ioException) {
Log.e("", "Error in getting address for the location");
}
if (addresses == null || addresses.size() == 0) {
msg = "No address found for the location";
sendResultsToReceiver(1, msg);
} else {
Address address = addresses.get(0);
StringBuffer addressDetails = new StringBuffer();
addressDetails.append(address.getFeatureName());
addressDetails.append("\n");
addressDetails.append(address.getThoroughfare());
addressDetails.append("\n");
addressDetails.append("Locality: ");
addressDetails.append(address.getLocality());
addressDetails.append("\n");
addressDetails.append("County: ");
addressDetails.append(address.getSubAdminArea());
addressDetails.append("\n");
addressDetails.append("State: ");
addressDetails.append(address.getAdminArea());
addressDetails.append("\n");
addressDetails.append("Country: ");
addressDetails.append(address.getCountryName());
addressDetails.append("\n");
addressDetails.append("Postal Code: ");
addressDetails.append(address.getPostalCode());
addressDetails.append("\n");
sendResultsToReceiver(2,addressDetails.toString());
}
}
//to send results to receiver in the source activity
private void sendResultsToReceiver(int resultCode, String message) {
Bundle bundle = new Bundle();
bundle.putString("address_result", message);
addressResultReceiver.send(resultCode, bundle);
}
}
3.XML To display your Data
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AddressListActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:id="#+id/add_label"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:text="Current Location Address"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_marginTop="16dp"
android:id="#+id/current_address"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:lines="10"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/add_label"/>
</android.support.constraint.ConstraintLayout>
For more, check out this link.
I've been working on a weather app, and I know it's not perfect (I'm just starting out with android development), but I'm not sure how to update the weather info on every startup. I tried to keep everything in the onCreate() method, but it just "sticks" on the location and conditions that I used when I first started.
I have been able to work around this with a button that gets the new location and weather conditions when it is pressed, but that's not very intuitive. I'm wondering how I can get new conditions on app startup. Might it involve calling onRestart()?
Here's my only activity in the app:
package com.photonfighterlabs.dropweather;
import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.ftoslab.openweatherretrieverz.CurrentWeatherInfo;
import com.ftoslab.openweatherretrieverz.OpenWeatherRetrieverZ;
import com.ftoslab.openweatherretrieverz.WeatherCallback;
import com.ftoslab.openweatherretrieverz.WeatherUnitConverter;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnSuccessListener;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class WeatherActivity extends Activity {
private double lat;
private double lng;
private String temp;
private String icon;
private TextView tempTextView;
private TextView cityTextView;
private TextView conditionsTextView;
private int LOCATION_PERMISSION_ID = 1001;
CurrentWeatherInfo currentWeatherInfoF;
private String city;
private List<Address> addresses;
private FusedLocationProviderClient mFusedLocationClient;
OpenWeatherRetrieverZ retriever;
ImageView image;
Geocoder geocoder;
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_weather);
retriever = new OpenWeatherRetrieverZ(API_KEY); // hidden for obvious reasons, but working
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
geocoder = new Geocoder(this, Locale.getDefault());
tempTextView = (TextView) findViewById(R.id.temp_text_view);
cityTextView = (TextView) findViewById(R.id.city_text_view);
conditionsTextView = (TextView) findViewById(R.id.conditions_text_view);
image = (ImageView) findViewById(R.id.conditions_image);
if (ContextCompat.checkSelfPermission(WeatherActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(WeatherActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_ID);
return;
}
mFusedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
#Override
public void onSuccess(Location location) {
if (location != null)
lat = location.getLatitude();
lng = location.getLongitude();
try {
addresses = geocoder.getFromLocation(lat, lng, 1);
} catch (IOException e) {
e.printStackTrace();
}
if (!addresses.isEmpty()) {
city = addresses.get(0).getLocality();
cityTextView.setText("Current Weather - " + city);
Log.d("City", city);
}
Log.d("LAT", String.valueOf(lat));
Log.d("LNG", String.valueOf(lng));
}
});
retriever.updateCurrentWeatherInfo(lat, lng, new WeatherCallback() {
#Override
public void onReceiveWeatherInfo(CurrentWeatherInfo currentWeatherInfo) {
currentWeatherInfoF = WeatherUnitConverter.convertToImperial(currentWeatherInfo);
}
#Override
public void onFailure(String error) {
Toast.makeText(WeatherActivity.this, error, Toast.LENGTH_SHORT).show();
}
});
}
public void onRetrieveButtonClick(View view) {
mFusedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
#Override
public void onSuccess(Location location) {
if (location != null)
lat = location.getLatitude();
lng = location.getLongitude();
try {
addresses = geocoder.getFromLocation(lat, lng, 1);
} catch (IOException e) {
e.printStackTrace();
}
if (!addresses.isEmpty()) {
city = addresses.get(0).getLocality();
cityTextView.setText("Current Weather - " + city);
Log.d("City", city);
}
Log.d("LAT", String.valueOf(lat));
Log.d("LNG", String.valueOf(lng));
}
});
retriever.updateCurrentWeatherInfo(lat, lng, new WeatherCallback() {
#Override
public void onReceiveWeatherInfo(CurrentWeatherInfo currentWeatherInfo) {
currentWeatherInfoF = WeatherUnitConverter.convertToImperial(currentWeatherInfo);
}
#Override
public void onFailure(String error) {
Toast.makeText(WeatherActivity.this, error, Toast.LENGTH_SHORT).show();
}
});
temp = currentWeatherInfoF.getCurrentTemperature();
Log.d("TMP : ", String.valueOf(temp));
tempTextView.setText( String.valueOf((int) Double.parseDouble(temp)) + (char) 0x00B0);
conditionsTextView.setText(currentWeatherInfoF.getWeatherDescriptionLong());
String iconURL = currentWeatherInfoF.getWeatherIconLink().toString();
Pattern p = Pattern.compile("\\d\\w(n|d)");
Matcher m = p.matcher(iconURL);
if (m.find()) {
icon = m.group();
Log.d("ICON", icon);
String iconName = "r" + icon;
image.setImageResource(getResources().getIdentifier(iconName, "drawable", getPackageName()));
Log.d("NAME", iconName);
}
}
}
getLastLocation() and updateCurrentWeatherInfo(...) are both asynchronous operations. You start them both at the same time which means that updateCurrentWeatherInfo will most likely run before the position is available.
You must start it only after you have got the position, for example from the onSuccess listener.
You have tried with onResume() method?
You can use it and when the view is visible wi
Awareness offers a snapshot-API for obtaining the weather via getWeather() API method at the device location without managing location access or integrating with APIs that query weather from server on-demand.
Illustrative code-snippet:
Awareness.SnapshotApi.getWeather(mGoogleApiClient)
.setResultCallback(new ResultCallback<WeatherResult>() {
#Override
public void onResult(#NonNull WeatherResult weatherResult) {
if (!weatherResult.getStatus().isSuccess()) {
Log.e(TAG, "Could not get weather.");
return;
}
Weather weather = weatherResult.getWeather();
Log.i(TAG, "Weather: " + weather);
}
});
Please refer here for description of the Weather class whose instance is returned by the API.
The advantage of using this is that you can massively simplify your code, avoid having to manage location requests and call the API as often as needed (I believe it has a cache to avoid too many network requests that are battery draining).
I am trying to get the user's location, more specifically their city. The app asks for permission at runtime. But when it tries to put the location into an ArrayList, it gives a null object reference error.
I will comment the specific line below.
What am I doing wrong?
ERROR
Attempt to invoke virtual method 'java.util.List android.location.Geocoder.getFromLocation(double, double, int)' on a null object reference
import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class PhoneList extends AppCompatActivity implements LocationListener {
LocationManager locationManager;
private String provider;
public final static int MY_PERMISSIONS_REQUEST_READ_LOCATION = 1;
public double myLng;
public double myLat;
List addresses = new ArrayList();
public Geocoder myGeo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_phone_list);
if (1 == 0) {
showAlert();
} else {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
GetLocationPermission();
return;
}
locationManager.requestLocationUpdates(provider, 0, 0, this);
Location location = locationManager.getLastKnownLocation(provider);
onLocationChanged(location);
myLat = location.getLatitude();
myLng = location.getLongitude();
}
}
private void showAlert() {
final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Enable Location")
.setMessage("Your Locations Settings is set to 'Off'.\nPlease Enable Location to " +
"use this app")
.setPositiveButton("Location Settings", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(myIntent);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
}
});
dialog.show();
}
public void GetLocationPermission(){
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSIONS_REQUEST_READ_LOCATION);
}
}
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
System.out.println("Yes");
} else {
System.out.println("boo");
}
return;
}
}
}
#Override
public void onLocationChanged(Location location) {
try {
//ERROR IS ON THIS LINE
addresses = myGeo.getFromLocation(location.getLatitude(), location.getLongitude(), 10);
} catch (IOException e) {
Log.e("LocateMe", "Could not get Geocoder data", e);
}
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
}
Looks like you haven't initialized your myGeo reference.
To fix it, just initialize it before you call getFromLocation(). As a side note, you should also make sure that the Location is not null, since the locationManager.getLastKnownLocation() call has the potential to return null.
#Override
public void onLocationChanged(Location location) {
try {
//Add initialization:
myGeo = new Geocoder(this, Locale.getDefault());
//Make sure that the Location is not null:
if (location != null) {
addresses = myGeo.getFromLocation(location.getLatitude(), location.getLongitude(), 10);
}
} catch (IOException e) {
Log.e("LocateMe", "Could not get Geocoder data", e);
}
}