Answer the question
In order to leave comments, you need to log in
How to correctly request permissions in the service?
There is a code for determining coordinates over the network:
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// Toast toast = Toast.makeText(this, "Permission failed", Toast.LENGTH_SHORT);
// toast.show();
return;
} else {
// Toast toast = Toast.makeText(this, "is work", Toast.LENGTH_SHORT);
// toast.show();
}
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
// GPS_PROVIDER отправить GPS
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new MyListener());
Process: com.example.gps, PID: 10673
java.lang.RuntimeException: Unable to instantiate service com.example.gps.MyService: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3925)
at android.app.ActivityThread.access$1500(ActivityThread.java:220)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1868)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7319)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.content.ContextWrapper.getSystemService(ContextWrapper.java:752)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question