0
BrodcastReceiverが運営していて、私が要求したGPSデータを私に渡してくれたサービスを持っていました。私はそれをやり遂げた後に停止しましたが、要求したデータを私に提供していませんでした。あまりにも停止していない、すべてのボディは、私の問題を考え出すに私を助けることができる コードは以下であり、サービスは実際のデバイスでテスト:サービスが動作しませんか?
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.os.SystemClock;
import android.widget.Toast;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
public class GpsService extends Service implements LocationListener{
static LocationManager mlocManager;
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void onCreate() {
}
@Override
public void onDestroy() {
}
@Override
public void onStart(Intent intent, int startid) {
mlocManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0, 0,this);
}
@Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
String Text = "My current location is: " +"Latitud = " + loc.getLatitude() +"Longitud = " + loc.getLongitude();
Toast.makeText(getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
// Here to stop the service and make it finish its task
SystemClock.sleep(40000);
// stop the Gps system by this application
mlocManager.removeUpdates(this);
//Here to stop the service by itself
stopSelf();
}
@Override
public void onProviderDisabled(String provider)
{
Toast.makeText(getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider)
{
Toast.makeText(getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
これは、SystemClock.sleep(40000)を使用するのは良い方法ではありません。待つための タイマーを使用してより良い結果を得る –