How enforce Android to re-read GPS position? - java

I try to write application, which shows GPS coordinates on button click.
#Override
public void onClick(View arg0) {
EditText editText;
editText = (EditText) findViewById(R.id.editText1);
LocationManager locationManager;
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation("gps");
editText.setText(location.toString());
}
Is problem: while in first time I got coordinates of location with time of GPS reading, second click get the same location. Even when I stop program and run again, it get the same location!
How enforce to refresh reading GPS?

while in first time I got coordinates of location with time of GPS reading, second click get the same location
That is not surprising.
Even when I stop program and run again, it get the same location!
That too is not surprising.
How enforce to refresh reading GPS?
Generally speaking, you don't. There is no requirement that the device has to have a fresh update just because you request the last-known location. It may be physically impossible to provide a new location fix. And, if you are not calling requestLocationUpdates(), it is very possible for getLastKnownLocation() to simply return null.
Your parameters to requestLocationUpdates() may have some impact for how aggressively the device tries to update GPS, versus saving power. However, there are no guarantees.

Try to get best provider that available to fetch the Location data ,
using this code before you get the location
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, false);
and in your getLastKnownLocation pass the best Location Provider to get the location like this
Location location = locationManager.getLastKnownLocation(bestProvider );

Related

"Access denied finding property 'camera.hal1.packagelist'" error thrown when open QR Scanner Intent on LG V30?

Coming from this question, here
I am currently developing an application that needs to scan QR Codes via the Device's built-in camera, using the following libraries:
implementation 'com.google.zxing:core:3.3.3'
implementation 'com.journeyapps:zxing-android-embedded:3.5.0'
The logic I am using to scan the QR Code is as follows, which has been taken from this tutorial:
IntentIntegrator integrator = new IntentIntegrator(QR_Activity.this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
integrator.setPrompt("Scan");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
This code is being called inside a Button using View.OnClickListener(), and the result is being handled by onActivityResult
Whenever I run this code and get the result back from the scanner, the scanner returns nothing, and a exception is written to the console, namely Access denied finding property "camera.hal1.packagelist". Permissions have been successfully granted to the application. The permissions in question are the CAMERA and INTERNET permissions.
The test device in question is a LG V30+ ThinQ Smartphone.
Is there a way to get around this issue? Or is this problem prevalent on all smartphones that have a dual camera setup, and therefore 'unfixable'?
UPDATE
Conducting a simple test using the following code:
/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
(The code has been taken from this answer from the above post.)
The above code yields the same results as the IntentIntegrator code shown above, and throws the same Access denied finding property "camera.hal1.packagelist" error as before.
After a lot of deliberation and a lot of experimentation, I have found a solution to this problem. The solution seems to come from a new declaration of the IntentIntegrator Class every time the QR Scanning Button is clicked. By this, I mean that the access to the camera is granted using the following line
new IntentIntegrator(MyActivity.this).initiateScan();
From my understanding, declaring the optional parameters of the object's instantiation explicitly (i.e. setBeepEnabled and so on) is causing the problem, and therefore denies the application permission to actually use the camera, even though permissions to the hardware have already been granted beforehand.
Building on this, the optional parameters should be declared and set within the IntentIntegrator declaration, and not explicitly on different lines. This way, the application still retains access to the camera hardware, but can now cater for other optional parameters and features. By this, I mean the parameters should be specified as the following:
new IntentIntegrator(MyActivity.this).setBeepEnabled(false).initiateScan();
This fix appears to work on all dual camera setups

How to make my app receive location intents like Google Maps

If you opened a link like this on your android phone Google maps will automatically open and show you the location on it's map.
I want to make my app receive intents if the user opened a link like this and also i want to get the lat,long from that link.
Is this Possible ?
Assuming the link is stored as String variable link
Now,
String[] split_link = link.split(Pattern.quote("#"));
String[] post_link = split_link[1].split(Pattern.quote(","));
double lattitude = Double.parseDouble(post_link[0]);
double longitude = Double.parseDouble(post_link[1]);
Try This!!

Allow mock Locations #Android

I have been trying to use this feature available under the developer options on a android device. i am a tester so trying to fake a location on android device. I have already given permission ACCESS_MOCK_LOCATION under android manifest permissions. Not sure what and where should i put my fake longitude and latitude in my code. does anyone have tried it before? I am new to code and a tester so don't have idea what code should i write for fake long and lat and where should i put it. I have the source code for my test app and its a health app with maps on the home screen.
After instantiating your LocationClient you can call locationclient.setMockMode(true);
After that you can have your code generate Location objects like so
public Location createLocation(double lat, double lng, float accuracy) {
// Create a new Location
Location newLocation = new Location(PROVIDER);
newLocation.setLatitude(lat);
newLocation.setLongitude(lng);
newLocation.setAccuracy(accuracy);
return newLocation;
}
After that you could do something like
Location testLocation = createLocation(12.34, 45.679, 9.0f);
and
locationClient.setMockLocation(testLocation);
(This is taken from here)
This blog describes how to use mock locations when debugging and turning them off when not debugging.

How to check if it is the first time a user has used my app

I am wondering what the best method is to check if it is the first time a certain user has used my app, and also when they update will it reset and make it think they are a new user.
Edit(For more precision): I want to store a user id number in my sql server, and I want every user to create a password when they first download, but I also want them to have the option to select "Already have account" so that they can sync their accounts across devices
Like Illegal Argument mentions, the most reliable way is to use a server, because if you use a Shared preference, the user can delete the data or move to a different device and appear to be a totally new user.
If you don't want to go through the trouble of creating and managing your own server, you might consider using Google's Cloud Save API. When your app starts, check for existing data in the cloud. If there's nothing there it's a new user so create the data. It should be as simple as that.
The most reliable way to know that the user is a first time user is via a server validation. Another way to do it would be to use shared preference and saving certain data in internal or external storage. However storing data in mobile is not so foolproof as data can be easily deleted by user.
Globals
String _StrCode="";
SharedPreferences preferences;
String _responseCode=null;
In oncreate but before set content view check
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
preferences = getSharedPreferences("mypref", 0);
_StrCode= preferences.getString("Code", _StrCode);
if(_StrCode.equalsIgnoreCase("")){
setContentView(R.layout.activity_qdc_status__launcher);
// here you can assign a value to
_StrCode ="some value";
Editor prefsEditor = preferences .edit();
prefsEditor.putString("Code",_StrCode);
prefsEditor.commit();
}else{
Intent _in= new Intent(firstscreen,secondscreen.class);
startActivity(_in);
finish();
}
}
i hope this will halp you and its easy to understand too.

Directions via Intent Automatically

I building an app that contains many addresses. A user can select an address in a listview. This should automatically show a user directions to that address from the users current location.
String uri = "geo:"+ selectedAddress.getLat+","+getLng();
StartActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(uri)));
I know all this does is show the location of the specified co-ords.
How would i go about showing the directions to the location from the users current location?
How would i go about showing the directions to the location from the users current location?
As #Frxstrem indicated, there is no documented and supported Intent to bring up directions.
ask the user for the place and do the following way:
String place="";//user gives the place.
String url = "http://maps.google.com/maps?daddr="+place;
Intent drn = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(url));
startActivity(drn);
Google will automatically take the current user location as starting point and give directions

Categories

Resources