2016-08-11 6 views
0

私はAndroid関連のアプリケーションをすべてのアフィリエイトの場所で見ることができますが、アプリケーションが閉じられると、サービスとしてロケーションを送信する必要があります。 Ex。 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; 
} 

}

Logcat:

E/AndroidRuntime:FATAL例外:メイン プロセス:br.com.testtotal、PID:2390 java.lang.RuntimeException:サービスをインスタンス化できませんbr.com.testtotal.servicos.servicoEnviaLocalizacao:java.lang.NullPo android.app.ActivityThread $ H.handleMessageでandroid.app.ActivityThread.access $ 1800(ActivityThread.java:135) でandroid.app.ActivityThread.handleCreateService(ActivityThread.java:2556) でinterException (ActivityThread.java: 1278) android.os.Handler.dispatchMessage(Handler.java:102) (android.os.Looper.loop(Looper.java:136) at 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 android.content.ContextWrapper.getSystemServiceで.NullPointerException (ContextWrapper.java:540)br.com.testtotal.servicos.servicoEnviaLocalizacaoで 。(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 (Nativeメソッド) 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(ネイティブメソッド)

+1

logcatから例外とスタックトレースを送信する必要があります。 –

答えて

0

サービスでは、コンテキストとサービス自体がクラスのその部分で起動されるため、コンストラクタを作成してデフォルトのオブジェクトをオーバーライドしないでください。常にデフォルトのままです。代わりにonCreate()メソッドをオーバーライドします。これは他のすべてが既に初期化された後にServiceが作成されるたびに呼び出されます。

関連する問題