2016-08-11 2 views
0

관련 제휴 업체의 위치를 ​​볼 수있는 Android 응용 프로그램이 있지만 응용 프로그램이 닫히면 위치를 서비스로 보내야합니다. 전의. Whatsapp, 앱을 닫으면 메시지가 수신됩니다.안드로이드 응용 프로그램에서 서비스로 위치를 실행할 때 오류가 발생했습니다.

i는 서비스 클래스를 만들 수 있지만 내가 전화 할 때이 응용 프로그램은 정지하지만,이 코드는 활동에 잘 작동 :

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; 
} 

}

로그 캣 :

E/AndroidRuntime : FATAL 예외 : 주 프로세스 : br.com.testtotal, PID : 2390 java.lang.RuntimeException : 서비스를 인스턴스화 할 수 없습니다. br.com.testtotal.servicos.servicoEnviaLocalizacao : java.lang.NullPo interException android.app.ActivityThread.handleCreateService (ActivityThread.java:2556) at android.app.ActivityThread.access $ 1800 (ActivityThread.java:135) at android.app.ActivityThread $ H.handleMessage (ActivityThread.java : 1,278) android.os.Handler.dispatchMessage (Handler.java:102) android.os.Looper.loop (Looper.java:136) android.app.ActivityThread.main에서 에서 (ActivityThread.java:5017에서 ) at java.lang.reflect.Method.invokeNative (네이티브 메소드) at java.lang.reflect.Method.invoke (Method.java:515) at com.android.internal.os.ZygoteInit $ Metho dAndArgsCaller.run (ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:595) at dalvik.system.NativeStart.main (기본 메소드) 발생 원인 : java.lang br.com.testtotal.servicos.servicoEnviaLocalizacao에서 android.content.ContextWrapper.getSystemService (ContextWrapper.java:540) 에서 .NullPointerException . (servicoEnviaLocalizacao.java:30) java.lang.Class.newInstanceImpl에서 (기본 방법) 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) android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1278) android.os.Handler.dispatchMessage (Handler.java:102) at에 있습니다. android.os.Looper.loop (Looper.java:136) android.app.ActivityThread.main (ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative (네이티브 메소드) at java.lang .reflect.Method.invoke (Method.java:515) 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 (네이티브 메서드)

+1

logcat에서 예외 및 스택 추적을 게시해야합니다. –

답변

0

서비스에서 컨텍스트 및 서비스 자체가 클래스의 해당 부분에서 시작되므로 서비스에서 기본 컨텍스트를 재정의하는 생성자를 만들지 않아야합니다. 항상 기본 설정으로 유지됩니다. 대신 onCreate() 메서드를 재정의하십시오. 다른 모든 것들이 이미 초기화 된 후에 Service이 생성 될 때마다 호출됩니다.