0
votes

I have an Android application for view any locations of affiliates, but when app is closed I need send location as a service. Ex. Whatsapp, messages are received when app is closed.

i create a service class, but when i call this application is stopped, but this code works fine on activity:

public class servicoEnviaLocalizacao extends Service {

private Timer timer = new Timer();
LocationManager lm;
LocationListener ll;

public servicoEnviaLocalizacao() {
    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    ll = new localizacao_background();
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 10, (android.location.LocationListener) ll);
}



@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {


    return START_NOT_STICKY;
}

}

Logcat:

E/AndroidRuntime: FATAL EXCEPTION: main Process: br.com.testtotal, PID: 2390 java.lang.RuntimeException: Unable to instantiate service br.com.testtotal.servicos.servicoEnviaLocalizacao: java.lang.NullPointerException at android.app.ActivityThread.handleCreateService(ActivityThread.java:2556) at android.app.ActivityThread.access$1800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at android.content.ContextWrapper.getSystemService(ContextWrapper.java:540) at br.com.testtotal.servicos.servicoEnviaLocalizacao.(servicoEnviaLocalizacao.java:30) at java.lang.Class.newInstanceImpl(Native Method) at java.lang.Class.newInstance(Class.java:1208) at android.app.ActivityThread.handleCreateService(ActivityThread.java:2553) at android.app.ActivityThread.access$1800(ActivityThread.java:135)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:136)  at android.app.ActivityThread.main(ActivityThread.java:5017)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)  at dalvik.system.NativeStart.main(Native Method) 

1
You need to post the exception and stacktrace from logcat.David Wasser

1 Answers

0
votes

In a service, you should never create a constructor to override the default one because the context and service itself are launched in that part of the class, so it should always remain the default one. Instead, override the onCreate() method. This will get called whenever the Service is created after everything else have already been initialized.