2016-10-14 11 views
1

私はこれを使用していますSmartLocationライブラリ。私はコードを使って活動中のサービスを停止することができます。ロケーションリスナーアンドロイドを停止できませんか?

SmartLocation.with(this).location().stop(); 

しかし、私はサービスでこのライブラリを使用する場合、私が開始することですが、私は、アプリを閉じるまで、それは停止しません。私がstopService()を使用したときにもう一つ注意すべきことがあります。

OnDestroy()は、サービスコードの

残りはここ

public class locationService extends Service implements OnLocationUpdatedListener, OnActivityUpdatedListener 
{ 
    private LocationGooglePlayServicesProvider provider; 

    NotificationCompat.Builder builder; 
    NotificationManager notificationManager; 

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

    @Override 
    public boolean onUnbind(Intent intent) { 
     return super.onUnbind(intent); 
    } 

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

     provider = new LocationGooglePlayServicesProvider(); 
     provider.setCheckLocationSettings(true); 

     SmartLocation smartLocation = new SmartLocation.Builder(getApplicationContext()).logging(true).build(); 

     smartLocation.location(provider).continuous().start(this); 
     smartLocation.activity().start(this); 

     return START_NOT_STICKY; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
    } 

    @Override 
    public boolean stopService(Intent name) { 
     return super.stopService(name); 
    } 

    @Override 
    public void onDestroy() { 
     stop(); 
    } 

    @Override 
    public void onLocationUpdated(Location location) { 
     double lat = location.getLatitude(); 
     double lng = location.getLongitude(); 
    } 

    @Override 
    public void onActivityUpdated(DetectedActivity detectedActivity) { 
    } 

    public void stop() 
    { 
     SmartLocation.with(this).location().stop(); 
     stopForeground(true); 
    } 
} 

答えて

0

で呼ばなっている私は、サービスを使用していて停止させながら、私はそれが働いていなかった、行動認識を使用していました。世界は、私は以下の私のコード

public class locationService extends Service implements OnLocationUpdatedListener, OnActivityUpdatedListener 
{ 
    private LocationGooglePlayServicesProvider provider; 

    NotificationCompat.Builder builder; 
    NotificationManager notificationManager; 
    SmartLocation smartLocation; 

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

    @Override 
    public boolean onUnbind(Intent intent) { 
     return super.onUnbind(intent); 
    } 

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

     provider = new LocationGooglePlayServicesProvider(); 
     provider.setCheckLocationSettings(true); 

     smartLocation = new SmartLocation.Builder(getApplicationContext()).logging(true).build(); 

     smartLocation.location(provider).continuous().start(this); 
     smartLocation.activity().start(this); 

     return START_NOT_STICKY; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
    } 

    @Override 
    public boolean stopService(Intent name) { 
     return super.stopService(name); 
    } 

    @Override 
    public void onDestroy() { 
     stop(); 
    } 

    @Override 
    public void onLocationUpdated(Location location) { 
     double lat = location.getLatitude(); 
     double lng = location.getLongitude(); 
    } 

    @Override 
    public void onActivityUpdated(DetectedActivity detectedActivity) { 
    } 

    public void stop() 
    { 
     smartLocation.location().stop(); 
     stopForeground(true); 
    } 
} 
に変更され、この smartLocation.location().stop();

のようにそれを停止したように、私はsmartlocationインスタンスを宣言しました

関連する問題