Jump to content

****Help with making an android app****

Go to solution Solved by Psynapse_231,

Omg I totally forgot that I had posted here, I am so sorry, I'll just reply and end this thread. I solved this, i'll discuss the solution too.

On 4/2/2020 at 4:14 PM, Kilrah said:

Do you actually have good GPS reception?

Yes I do get good reception, google maps uses the same basic API and it gives location in seconds

On 4/5/2020 at 5:43 AM, lal12 said:

You could try it in the Android emulator, where you can set arbitrary GPS inputs, there you could determine whether it is a problem on your app or your GPS reception. Or use a Fake Location app, which can help debug things like that.

I tried that, but didn't work. The issue was that I wasn't calling the location as an open method, so it would get location and speed data only when the method was called. I used the functions on application level method, and it worked just fine.

I am making a speedometer app which shows the device speed from getSpeed(). But I keep getting the location as null. I have added the permissions in the manifest file:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

And, checking the location permissions returns that the loction is available:

LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != 
        PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 
        Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            TextView loc =(TextView) findViewById(R.id.textView3);
                    loc.setText("LocationUnvailable");
            Toast.makeText(MainActivity.this, "First enable LOCATION ACCESS in settings.", 
        Toast.LENGTH_LONG).show();
        } else {
            TextView loc =(TextView) findViewById(R.id.textView3);
            loc.setText("LocationAvailable");
        }
         lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
         this.onLocationChanged(null);

But the element that is supposed to show me the speed of the device shows that the location is null:

public void onLocationChanged(Location location) {
    if (location==null){
        // if you can't get speed because reasons :)
        TextView spd = (TextView) findViewById(R.id.textView);
                spd.setText("Location is null");
    }
    else{
        //int speed=(int) ((location.getSpeed()) is the standard which returns meters per second. In 
this example i converted it to kilometers per hour

        int speed=(int) ((location.getSpeed()*3600)/1000);
        TextView spd = (TextView) findViewById(R.id.textView);
        spd.setText(speed+" km/h");
    }
}

How do I fix this problem? Is there any more permissions I must add? If above snippets dont have enough information, see the full code below:

package com.example.prototype22;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class MainActivity extends AppCompatActivity implements LocationListener {

@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            TextView loc =(TextView) findViewById(R.id.textView3);
                    loc.setText("LocationUnvailable");
            Toast.makeText(MainActivity.this, "First enable LOCATION ACCESS in settings.", 
Toast.LENGTH_LONG).show();
        } else {
            TextView loc =(TextView) findViewById(R.id.textView3);
            loc.setText("LocationAvailable");
        }
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    this.onLocationChanged(null);

}

@SuppressLint("SetTextI18n")
@Override
public void onLocationChanged(Location location) {
    if (location==null){
        // if you can't get speed because reasons :)
        TextView spd = (TextView) findViewById(R.id.textView);
                spd.setText("Location is null");
    }
    else{
        //int speed=(int) ((location.getSpeed()) is the standard which returns meters per second. In this example i converted it to kilometers per hour

        int speed=(int) ((location.getSpeed()*3600)/1000);
        TextView spd = (TextView) findViewById(R.id.textView);
        spd.setText(speed+" km/h");
    }
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

@Override
public void onPointerCaptureChanged(boolean hasCapture) {

}
}

Help would be really appreciated :)

Link to comment
Share on other sites

Link to post
Share on other sites

Do you actually have good GPS reception?

F@H
Desktop: i9-13900K, ASUS Z790-E, 64GB DDR5-6000 CL36, RTX3080, 2TB MP600 Pro XT, 2TB SX8200Pro, 2x16TB Ironwolf RAID0, Corsair HX1200, Antec Vortex 360 AIO, Thermaltake Versa H25 TG, Samsung 4K curved 49" TV, 23" secondary, Mountain Everest Max

Mobile SFF rig: i9-9900K, Noctua NH-L9i, Asrock Z390 Phantom ITX-AC, 32GB, GTX1070, 2x1TB SX8200Pro RAID0, 2x5TB 2.5" HDD RAID0, Athena 500W Flex (Noctua fan), Custom 4.7l 3D printed case

 

Asus Zenbook UM325UA, Ryzen 7 5700u, 16GB, 1TB, OLED

 

GPD Win 2

Link to comment
Share on other sites

Link to post
Share on other sites

You could try it in the Android emulator, where you can set arbitrary GPS inputs, there you could determine whether it is a problem on your app or your GPS reception. Or use a Fake Location app, which can help debug things like that.

Link to comment
Share on other sites

Link to post
Share on other sites

  • 6 months later...

Omg I totally forgot that I had posted here, I am so sorry, I'll just reply and end this thread. I solved this, i'll discuss the solution too.

On 4/2/2020 at 4:14 PM, Kilrah said:

Do you actually have good GPS reception?

Yes I do get good reception, google maps uses the same basic API and it gives location in seconds

On 4/5/2020 at 5:43 AM, lal12 said:

You could try it in the Android emulator, where you can set arbitrary GPS inputs, there you could determine whether it is a problem on your app or your GPS reception. Or use a Fake Location app, which can help debug things like that.

I tried that, but didn't work. The issue was that I wasn't calling the location as an open method, so it would get location and speed data only when the method was called. I used the functions on application level method, and it worked just fine.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×