0

I want to get current location in my app. I tried this solution.

I have to check permission, but I cannot find how to check permission nonActivity class.

I tried this:

public class MyLocation {

    public MyLocation(Context context){
        this.context = context;
    }

    public boolean getLocation(Context context, LocationResult result) {

        locationResult = result;
        if (lm == null)
            lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);



        if (gps_enabled)
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return true;
            }
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
        if (network_enabled)
            lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
        timer1 = new Timer();


        timer1.schedule(new GetLastLocation(context), 10000);

        return true;
    }



    class GetLastLocation extends TimerTask {
        Context context;

        public GetLastLocation(Context context){
           this.context = context;
        }
        @Override
        public void run() {

            //Context context = getClass().getgetApplicationContext();
            Location net_loc = null, gps_loc = null;
            if (gps_enabled)
                if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                    return;
                }gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if(network_enabled)
                net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        }
    }

}

In my Activity I passed context;

MyLocation myLocation = new MyLocation(this);

I am getting error in permission in GetLastLocation class ;

E/AndroidRuntime: FATAL EXCEPTION: Timer-1
                                                                               Process: com.example.merve.myapplication, PID: 4124
                                                                               java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Context.checkPermission(java.lang.String, int, int)' on a null object reference
                                                                                   at android.support.v4.content.ContextCompat.checkSelfPermission(ContextCompat.java:439)
                                                                                   at other.MyLocation$GetLastLocation.run(MyLocation.java:124)
                                                                                   at java.util.TimerThread.mainLoop(Timer.java:555)
                                                                                   at java.util.TimerThread.run(Timer.java:505)

MyLocation.java line 124:

            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && 
ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

I cannot add full code.

merve
  • 73
  • 2
  • 12
  • @sandy can you check my question, please. – merve Dec 23 '17 at 12:32
  • You can pass the `context` to the non-activity class which you want to do the check. Use the `context` then in that class to call methods that require `context` – Akram Dec 23 '17 at 12:46
  • @Merka I edited my question – merve Dec 23 '17 at 13:09
  • can you post the complete error log which tells the line number of the exception as well? Seems that the problem is you didn't initialize `context` field of `GetLastLocation` class. It is `null` when used. – Akram Dec 23 '17 at 13:29
  • I fixed this problem according to Leo Aso's answers. However, now I cannot open the app. There is no error, I can see lat and long in the console. @Merka – merve Dec 23 '17 at 13:49

2 Answers2

1

If I had to guess, I'd say this

    public GetLastLocation(Context context){
        context = this.context;
    }

should be

    public GetLastLocation(Context context){
        this.context = context;
    }
Leo Aso
  • 11,898
  • 3
  • 25
  • 46
  • Yeah, I fixed this, but now cannot open the app. İn console, I can see latitude and longitude. An there is no error – merve Dec 23 '17 at 13:47
  • When you say you can't open the app. do you mean it's crashing, or it's just not displaying anything? And , if it's crashing, is there a new error? – Leo Aso Dec 23 '17 at 14:00
  • It is not displaying anything. finish() is the reason.Thanks for your help. – merve Dec 23 '17 at 14:01
0

Simply pass your activity or context object to the non-activity class. The timertask doesn't run on the main thread so you have to ask for the permission somewhere else. Maybe before creating the GetLastLocation object

Niza Siwale
  • 2,390
  • 1
  • 18
  • 20